Jump to:

2002 Posts in 1422 Topics by 616 members

Form Questions

SilverStripe Forums » Form Questions » RequiredFields validation vs. Associative array - based input names

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Page: 1
Go to End
Author Topic: 397 Views
  • Recon
    Avatar
    Community Member
    1 Post

    RequiredFields validation vs. Associative array - based input names Link to this post

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

  • martimiz
    Avatar
    Forum Moderator
    1067 Posts

    Re: RequiredFields validation vs. Associative array - based input names Link to this post

    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...

    397 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.