We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 9439
    • 90 Posts
    I have a multi-page form. On the second page is a form within a table. Each row contains two select options that should be mandatory and two regular, non-mandatory fields. The form can have a number of rows, depending on a user choice on the previous page.

    One select option has a name attribute styled name[], the other has a name attribute styled make[]. Formit seems to ignore validate=`name[]:required` and `make[]:required`. If instead I specify that name[] and make[] needs to have more than 1 character, eg, validate=`name[]:minLength=^1^`, it prevents submission of the form if no drop down is chosen, but it also prevents it when a selection is made. Can't win!

    Can anyone suggest a solution?
    [[!FormItRetriever]]
    [[!FormIt?
    &hooks=`validate,redirect`
    &validate=`name[]:required`
    &redirectTo=`35`
    &store=`1`
    &submitVar=`go`
    ]]

    <td><select id="type" name="name[]">
    <option value="" [[!+fi.type:FormItIsSelected=``]]>choose device</option>
    <option value="Tablet" [[!+fi.type:FormItIsSelected=`Tablet`]]>Tablet</option>
    etc   
      David Heriot
      pfsmedia
      • 9439
      • 90 Posts
      So what I needed was not a prehook as I initially thought, but a custom validation hook. Something along the lines of
      foreach($value as $tvalue)
      {
      //$success = strlen($value[0]) > 1;
        if ($tvalue=='') {
          return $validator->addError($key,'***');
          break;
        }
      }
      return true;
      It worked after a fashion but only all rows weren't selected, i.e. didn't have a value. If one did, the form was validated. Now that's obviously down to my lack of PHP skills but in the end it just seemed easier to use Javascript - which was what I did. If anyone else is interested, I based it on the jquery.validate.js, used in this excellent little tutorial at http://www.codeboss.in/web-funda/2009/05/27/jquery-validation-for-array-of-input-elements/
        David Heriot
        pfsmedia