We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 6902
    • 126 Posts
    Just a quick little snippet I thought I’d share. This validator makes one field required based on the setting of another field. Feel to modify/improve however you want.

    I made it for some forms that have the standard "option 1", "option 2", "option 3", "other" radio/drop-down with a text field to enter something if the user selects "other". I only wanted the "other_text" to be required only if the user has selected "other" as their option.

    The Validator Call:
    The validator takes a parameter with two pipe-separated values.
    In this example, "other_text" will be required if the field "testfield" has a value of "testvalue"
    [[!FormIt? &validate=`other_text:condRequired=`testfield|testvalue` `]]
    


    The Snippet:
    <?php
    
    // 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]];
    }
    
    // is test field set to test value?
    if ($field == $param[1]) {
    	// is the required field empty?
    	$value = trim($value);
    	if (empty($value)) {
    		$validator->addError($key,'This field is required');
    		return false;
    	}
    }
    
    return true;
    
    ?>
    
      • 6902
      • 126 Posts
      I added to the code a bit to allow custom error messages. Simply include the custom error message as a third pipe-delimited option:

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


      <?php
      
      // 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';
      
      // is test field set to test value?
      if ($field == $param[1]) {
        // is the required field empty?
        $value = trim($value);
        if (empty($value)) {
          $validator->addError($key,$errorMssg);
          return false;
        }
      }
      
      return true;
      ?>
      
        • 6902
        • 126 Posts
        I also discovered a bug/caveat...

        The test field must appear before the validating field in the form. FormIt builds its list of field references as it validates them, so any field "below" one that you are validating doesn’t exist yet in the validator->fields array at the time of validation. That’s what I could glean from testing at any rate...

        If I can find a fix/workaround for this issue, then the snippet will provide a nice way to require one field or the other. In my case, there’s a form where we want either a phone or an email. This could be accomplished with the following:

        [[!FormIt?
           &validate=`email:condRequired=`phone||Please supply an email address and/or phone number`,
               phone:condRequired=`email||Please supply an email address and/or phone number` `]]
        


        However... the first validator will always fail, because the second field isn’t available to test against.

        Note: I would also be in favor of returning the built-in email validator to its previous behavior of allowing empty strings through. IMO this would make it much more useful. If it’s a required email address, then you can always use :email:required. As it is now, there’s no way to validate a non-required email address field.
          • 28215
          • 4,149 Posts
          Quote from: debussy at Jan 13, 2011, 02:22 PM

          Note: I would also be in favor of returning the built-in email validator to its previous behavior of allowing empty strings through. IMO this would make it much more useful. If it’s a required email address, then you can always use :email:required. As it is now, there’s no way to validate a non-required email address field.

          Can you file that as a ticket? Thanks!
            shaun mccormick | bigcommerce mgr of software engineering, former modx co-architect | github | splittingred.com
            • 6902
            • 126 Posts
            This looks like it was already filed... I upvoted and commented on that one...
              • 6902
              • 126 Posts
              For anyone reading this thread: Both the issues with email validation and the form field order have since been fixed (Thanks Guys!)
                • 15022
                • 31 Posts
                Thanks very much for this thread. It is very useful.
                  illegitimati non carborundum
                  • 41384
                  • 2 Posts
                  Great thread! Thanks a lot.
                    • 19389
                    • 11 Posts
                    Great Post. This is what I was looking for... but it seems it doesn't work with checkboxes, does it?

                    I tried to implement it as such:

                    [[!FormIt?
                        &hooks=`email`
                        
                        &validate=`workemail:blank,
                            fCheckDatenschutz:required,
                            fSurname:required,
                            fFirstname:required,
                            fStreetNr:required,
                            fPostalcodRegion:required,
                            fEmail:email:required,
                            fGeschenkSurname:condRequired=`fCheckGeschenkabo|ja|blablabla.` `
                    
                    ...
                    ]]
                    


                    My Checkbox:
                    <form>
                    ...
                    		<div class="formDiv clearfix formCheck">
                    			<label for="foptCheckGeschenkabo">Geschenkabo?</label>
                                <input name="fCheckGeschenkabo[]" type="hidden" value="" >
                    			<input id="foptCheckGeschenkabo" 
                    				name="fCheckGeschenkabo[]" 
                    				type="checkbox" 
                    				value="ja" 
                                    [[!+fi.fCheckGeschenkabo:FormItIsChecked=`ja`]] >
                    		</div>
                    ...
                    </form>
                    


                    I'm kind of a php-novice and can't seem to figure this out sad.
                      • 19389
                      • 11 Posts
                      :facepalm:

                      It seems to work now. (!!!)

                      I made the mistake of not adding
                      &customValidators=`condRequired`
                      and the right error tags... therefor I couldn't see if it spit out an error or not.

                      PS: wouldn't adding conditional validation in FormIt be a nice addition? Would be awesome if it could also check if a value is greater/smaller/etc. than another one, such as
                      field:condRequired=^testfield>testvalue^
                      or
                      field:condRequired=^testfield!=testvalue^