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   1420 Views

Avatar
ivo

Community Member, 19 Posts

21 January 2012 at 12:01am

Edited: 21/01/2012 12:06am

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

Community Member, 225 Posts

24 January 2012 at 9:24am

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

Avatar
ivo

Community Member, 19 Posts

26 January 2012 at 5:01am

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

Community Member, 59 Posts

26 January 2012 at 10:58pm

Edited: 26/01/2012 10:58pm

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

Community Member, 19 Posts

28 January 2012 at 4:14am

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

thanks both