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 /

RequiredFields validation vs. Associative array - based input names


Reply


2 Posts   546 Views

Avatar
Recon

Community Member, 1 Post

5 February 2014 at 3:55am

Hello,

I encounter a small problem (on SS 3.1) when using the RequiredFields class for validation on a Form object used in the front-end while the subject of validation is a set of inputs having the names in form of associative arrays. Due to the nature of the form, I have to loop over some data and add a dynamic number of inputs, over a few tabs, so they will look like

<input type="text" name="Location[32]">
<input type="text" name="Location[41]">

My code looks like this:

(...)

$validator = new RequiredFields('Name', 'Description');

// The validation over these fields will work fine
$fields->addFieldToTab("Root.General", TextField::create('Name', 'Nume *', $entity->Name)->addExtraClass('span6'));
$fields->addFieldToTab("Root.General", TextareaField::create('Description', 'Descriere * ', $description)->addExtraClass('span6'));

(...)

foreach(...){
   $fields->addFieldToTab("Root.Adresa ({$i})", TextField::create("Location[{$addressID}]", "Adresa Sediu *", $address->Location)->addExtraClass('span6'));
   
   // This won't work, it will simply move along even if the field is empty
   $validator->addRequiredField("Location[{$addressID}]");
}

(...)

$form = new Form($this, __FUNCTION__, $fields, $actions, $validator);
$form->setAttribute("novalidate", "novalidate");

return $form;

If I were to let the HTML5 validation turned on, the array-named input will have the correct "required" attribute (as a side note, I can't let the HTML5 validation enabled because, upon submission, the popup error might be raised in a hidden tab, and there's no easy way to detect via JS this kind of events... So I have to stick with old-style messages)

However, without the HTML5 validation, after submission there is no "The ... field is required" message on the array-named input, but works ok for the normal-named inputs.

I've also tried to use, but no joy:

$validator->addRequiredField("Location"); // no array keys
$validator->addRequiredField("Location[]"); // empty key
$validator->addRequiredField("Location['{$addressID}']"); // key encapsulated by apostrophe

Is there an easier work-around, which will not enforce the use of "$form->addErrorMessage(...)"?

Avatar
martimiz

Forum Moderator, 1132 Posts

8 February 2014 at 11:18am

Edited: 08/02/2014 11:18am

This seems to work fine for me in 3.1.2 on a frontend form (without the HTML5 validation):

$addresID = 27;

$fields = new FieldList(
   TextField::create('TestingThis[1]', 'Testing this'),
   TextField::create("TestingThis[{$addresID}]", 'Testing this too')
);
...
$validator = new RequiredFields( 'TestingThis[1]');
$validator->addRequiredField("TestingThis[{$addresID}]");

So I'm curious wahat the difference with your code would be...