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 thought I would share this for anyone who is interested... Basically I had a group of inputs (in this case, the street, city, state, and zip of an address) that I wanted to treat as one requirement, i.e., test at one time if any of the fields in the group are missing and then output a single error message. Instructions are in the comments. smiley

    //******************************************
    // Created by Darren Doyle, Aug. 2011
    //******************************************
    
    // ----- PURPOSE -----
    // This makes a group of form fields required
    // as a group and outputs a single error message.
    
    
    // ----- SETUP -----
    // 1) Don't forget to add the following attribute
    // to the FormIt snippet call:
    // —————————————————————————————————
    // &customValidators=`groupRequired`
    // —————————————————————————————————
    //
    // 2) Name all your group fields with one prefix:
    // —————————————————————————————————————————
    // <input name="myGroup_field1" val="" />
    // <input name="myGroup_field2" val="" />
    // <input name="myGroup_field3" val="" />
    // —————————————————————————————————————————
    //
    // 3) Call the validator in the FormIt snippet call
    // using just the prefix (be sure to include a leading "|"
    // if leaving the error message blank):
    // —————————————————————————————————————————————————————————————————————————————————————————
    // &validate=`myGroup:groupRequired=`Optional error message|_field1|_field2|_field3|...`
    // —————————————————————————————————————————————————————————————————————————————————————————
    //
    // 4) Place the error placeholder inline on just the prefix
    // —————————————————————————
    // [[!+fi.error.myGroup]]
    // —————————————————————————
    
    
    // parse parameters
    $param = explode('|',$param);
    
    // set the error message
    $errorMssg = !empty($param[0]) ? $param[0] : 'These fields are required';
    
    // test fields
    for ($i=1; $i<count($param); $i++) {
    	
    	// test if field is an array or not (work with options and other field types)
    	if ( is_array($validator->fields[$key.$param[$i]]) ) {
    		$field = $validator->fields[$key.$param[$i]][0];
    	} else {
    		$field = $validator->fields[$key.$param[$i]];
    	}
    	
    	// bounce if any field is empty
    	if (empty($field)) {
    		$validator->addError($key,$errorMssg);
    		return true;
    	}	
    }
    
    return true;