We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 36561
    • 38 Posts
    Allow for multiple values to be required

    With a select or radio-buttons, you may want to make one of many selections be required.

    So instead of this FormIt call:
    [[!FormIt? &validate=`other_text:condRequired=`testfield|testvalue|My custom error message.` `]]
    


    you do this:
    [[!FormIt? &validate=`other_text:condRequired=`testfield|testvalue++also_required++third-option|My custom error message.` `]]
    



    Here's the snippet-code:

    // get test field and value
    $param = explode('|',$param);
    
    // test if field is an array or not -- work with options and other field types
    if ( is_array($validator->fields[$param[0]]) ) {
      $field = $validator->fields[$param[0]][0];
    } else {
      $field = $validator->fields[$param[0]];
    }
     
    // set the error message
    $errorMssg = !empty($param[2]) ? $param[2] : 'This field is required';
    
    // allow multiple values
    $field_set = false;
    $multi_vals = explode('++', $param[1]);
    if (is_array($multi_vals) && count($multi_vals) > 1) {
      $field_set = (in_array($field, $multi_vals)) ? true : false;
    }
    else {
      $field_set = ($field == $param[1]) ? true : false;
    }
     
    // is test field set to test value?
    if ($field_set) {
      // is the required field empty?
      $value = trim($value);
      if (empty($value)) {
        $validator->addError($key,$errorMssg);
        return false;
      }
    }
    
    return true;
    
      • 11449
      • 28 Posts
      I have had no luck getting this or the other (requiredIf) validators to work, Example:

      Radio group "working":
      - Option one (value: 1)
      - Option two (value: 2)

      Radio group "work"
      - Option a
      - Option b

      What I want is for the "work" radio group to be required only when "working" radio has Option two selected (value: 2). Currently if I have option one selected work isn't required, which is great. But if I have Option two selected work is required, problem is it always complains that "work" is required even when I have selected Option a, or Option b within it.

      I have set up formit with:
      - &customValidators=`condRequired`
      - &validate=`work:condRequired=^working|2^`

      (I had to use ^ rather than ` in the validate call because ` are no longer allowed in formit validate calls)

      I have tried with all 3 versions of the condRequired snippet in this thread. Any help would be greatly appreciated. I have the exact same problem with the requiredIf custom validator located at https://gist.github.com/bertoost/4000623. I'm not a PHP guy and really need this required if kind of validation. Wish formit had this built in.
        • 4172
        • 5,888 Posts
        A simple solution to that issue could be wrapping formit into a custom-snippet like that:

        $working = $modx->getOption('working',$_REQUEST,'');
        if ($working == '2'){
            $scriptProperties['validate']='work:required';
        }
        return $modx->runSnippet('FormIt',$scriptProperties);


        call it the same way as you would call formit, just put all your validation-stuff into your custom - snippet.

        So instaed of

        [[FormIt? &yourproperties]]


        you call

        [[myFormIt? &yourproperties]]
          -------------------------------

          you can buy me a beer, if you like MIGX

          http://webcmsolutions.de/migx.html

          Thanks!