Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

Form Questions

Custom Form Validation Error in SESSION


Reply

5 Posts   1267 Views

Avatar
ivo

21 January 2012 at 12:01am (Last edited: 21 January 2012 12:06am), Community Member, 19 Posts

Hi,
i have a custom Form:

class ContactForm extends Form {...}

which gets passed a Validator

$validator = new RequiredFields('Name', 'Email', 'Comments');
return new ContactForm($this, 'ContactForm', $fields, $actions, $validator);

The validator seems to work because in the Session there are Errors:

array
'errors' =>
array
0 =>
array
'fieldName' => string 'Name' (length=4)
'message' => string '"Name*" wird benötigt.' (length=23)
'messageType' => string 'required' (length=8)

But in Form.php from sapphire the Message() method looks for 'formError' key and so the validation errors arent found in $Message:

$this->message = Session::get("FormInfo.{$this->FormName()}.formError.message");

How comes that the validation errors of the custom Form are stored in 'errors' instead of 'formError' in the SESSION?

Avatar
Devlin

24 January 2012 at 9:24am Community Member, 215 Posts

Semantics.
FormName.formError is the message of the form and FormName.errors[] are the messages of the fields.

Avatar
ivo

26 January 2012 at 5:01am Community Member, 19 Posts

Alright
I had expected them in FormName.formError, thou.
I thouht the Validator of the form would place them there automatically.

So they could be rendered as usual:

<% if Message %>
<p id="{$FormName}_error" class="message $MessageType">$Message</p>
<% else %>
<p id="{$FormName}_error" class="message $MessageType" style="display: none;"></p>
<% end_if %>

What do i have to do to get them there?

Avatar
Silverfish

26 January 2012 at 10:58pm (Last edited: 26 January 2012 10:58pm), Community Member, 59 Posts

Hi,

if I get you right you are looking for this:
http://www.silverstripe.org/form-questions/show/19073#post311384#post311384

Regards
SF

Avatar
ivo

28 January 2012 at 4:14am Community Member, 19 Posts

ok
i rendered the errors manually from FormName.errors[]
that works fine

thanks both