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(...)"?