kyled

Gravity Forms Conditional Required Fields PHP

Apr 3rd, 2012
252
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.49 KB | None | 0 0
  1. <?php
  2.  
  3. /*  PHP for required fields
  4. --------------------------------------------------------------------- */
  5.  
  6. add_filter("gform_validation", "my_validate_required_fields");
  7.  
  8. function my_validate_required_fields($validation_result) {
  9.  
  10.  
  11.     // set up some empty arrays
  12.    
  13.     // this will be a list of the required fields, as set in the first foreach statement below
  14.     $newly_required_fields = array();
  15.    
  16.     // This will be a comma separated list of the field IDs of the initial questions
  17.     $initial_questions = array();
  18.    
  19.     // This will be a comma separated list of the field IDs of the followup questions.  Each item will
  20.     // correspond with the items in the initial questions array, so they must be in the same position
  21.     $followup_questions = array(); 
  22.    
  23.     // trigger responses are the answers that the question must be set to to make followup question valid.
  24.     // These will correspond with the followup and initial questions arrays.
  25.     $trigger_responses  = array();
  26.  
  27.    
  28.     // an example with two different forms, 2 and 3.  Any other forms will skip validation
  29.     if ( $validation_result["form"]["id"] == 2 ) {
  30.         $initial_questions = array(11, 13, 20, 32);
  31.         $followup_questions = array(12, 14, 21, 33);
  32.         $trigger_responses = array('No', 'No', 'Very unlikely', 'Very unlikely');  
  33.     }  
  34.     else if ( $validation_result["form"]["id"] == 3 ) {
  35.         $initial_questions = array(22, 28);
  36.         $followup_questions = array(23, 29);
  37.         $trigger_responses = array('Very unlikely', 'Very unlikely');  
  38.     }
  39.     else {
  40.         return $validation_result;
  41.     }
  42.  
  43.    
  44.     // loop through fields to create a list of required fields ($newly_required_fields)
  45.     foreach( $validation_result["form"]["fields"] AS &$field ) {
  46.        
  47.         $field_value = null;
  48.        
  49.         // check if the field id is in the initial_questions array
  50.         if ( in_array($field['id'], $initial_questions) ) {
  51.            
  52.             // get position of field id in initial_questions
  53.             $position = array_search( $field['id'], $initial_questions );
  54.            
  55.             // get the current field value
  56.             $field_value = rgpost("input_{$field['id']}");
  57.            
  58.             // if the current field value exists and is equal to the trigger_response
  59.             // add the field id of the followup question to the newly_required_fields array
  60.             // so we can loop through the fields again later and mark the followups as required.
  61.             if ( $field_value && $field_value != '' && ($field_value == $trigger_responses[$position]) ) {
  62.                 $newly_required_fields[] = $followup_questions[$position];             
  63.             }
  64.  
  65.  
  66.         } // end if field in initial questions array
  67.        
  68.        
  69.     } // end first foreach fields
  70.    
  71.     // loop through fields again, and if the field ID is in newly_required_fields, set it as
  72.     // required, and mark the field as invalid
  73.     foreach($validation_result["form"]["fields"] AS &$field ) {
  74.         // check that the field ID is in newly_required_fields
  75.         if ( in_array($field['id'], $newly_required_fields) ) {
  76.             // set field to be required
  77.             $field['isRequired'] = 1;
  78.            
  79.             // get current value of the followup field
  80.             $field_value = rgpost("input_{$field['id']}");
  81.  
  82.             // if the followup field value is empty, we mark the field as invalid,
  83.             // set failed validation to true, and setup a validation message
  84.             if (!$field_value) {
  85.                 $validation_result['is_valid'] = false;
  86.                 $field["failed_validation"] = true;    
  87.                 $field["validation_message"] = 'This field is required.';
  88.             }
  89.        
  90.         } // end if field ID is in newly_required_fields
  91.        
  92.     } // end foreach fields
  93.    
  94.    
  95.     // return the updated validation_result
  96.     return $validation_result ;
  97. }
  98.  
  99.  
  100. ?>
Advertisement
Add Comment
Please, Sign In to add comment