Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /* PHP for required fields
- --------------------------------------------------------------------- */
- add_filter("gform_validation", "my_validate_required_fields");
- function my_validate_required_fields($validation_result) {
- // set up some empty arrays
- // this will be a list of the required fields, as set in the first foreach statement below
- $newly_required_fields = array();
- // This will be a comma separated list of the field IDs of the initial questions
- $initial_questions = array();
- // This will be a comma separated list of the field IDs of the followup questions. Each item will
- // correspond with the items in the initial questions array, so they must be in the same position
- $followup_questions = array();
- // trigger responses are the answers that the question must be set to to make followup question valid.
- // These will correspond with the followup and initial questions arrays.
- $trigger_responses = array();
- // an example with two different forms, 2 and 3. Any other forms will skip validation
- if ( $validation_result["form"]["id"] == 2 ) {
- $initial_questions = array(11, 13, 20, 32);
- $followup_questions = array(12, 14, 21, 33);
- $trigger_responses = array('No', 'No', 'Very unlikely', 'Very unlikely');
- }
- else if ( $validation_result["form"]["id"] == 3 ) {
- $initial_questions = array(22, 28);
- $followup_questions = array(23, 29);
- $trigger_responses = array('Very unlikely', 'Very unlikely');
- }
- else {
- return $validation_result;
- }
- // loop through fields to create a list of required fields ($newly_required_fields)
- foreach( $validation_result["form"]["fields"] AS &$field ) {
- $field_value = null;
- // check if the field id is in the initial_questions array
- if ( in_array($field['id'], $initial_questions) ) {
- // get position of field id in initial_questions
- $position = array_search( $field['id'], $initial_questions );
- // get the current field value
- $field_value = rgpost("input_{$field['id']}");
- // if the current field value exists and is equal to the trigger_response
- // add the field id of the followup question to the newly_required_fields array
- // so we can loop through the fields again later and mark the followups as required.
- if ( $field_value && $field_value != '' && ($field_value == $trigger_responses[$position]) ) {
- $newly_required_fields[] = $followup_questions[$position];
- }
- } // end if field in initial questions array
- } // end first foreach fields
- // loop through fields again, and if the field ID is in newly_required_fields, set it as
- // required, and mark the field as invalid
- foreach($validation_result["form"]["fields"] AS &$field ) {
- // check that the field ID is in newly_required_fields
- if ( in_array($field['id'], $newly_required_fields) ) {
- // set field to be required
- $field['isRequired'] = 1;
- // get current value of the followup field
- $field_value = rgpost("input_{$field['id']}");
- // if the followup field value is empty, we mark the field as invalid,
- // set failed validation to true, and setup a validation message
- if (!$field_value) {
- $validation_result['is_valid'] = false;
- $field["failed_validation"] = true;
- $field["validation_message"] = 'This field is required.';
- }
- } // end if field ID is in newly_required_fields
- } // end foreach fields
- // return the updated validation_result
- return $validation_result ;
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment