Advertisement
dochara

Unset or validate a field in a Gravity form

Jan 16th, 2012
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.41 KB | None | 0 0
  1. <?php
  2. # For Gravity Forms. Sometimes a form has a checkbox or radio button that is not always required - agree terms or confirm something.
  3. # In this case it is a radio button to confirm that the person submitting the form is insured before they sign up for a subscription.
  4. # Some supscriptions require that the user confirms insurance status, some do not.
  5. # These functions remove the field if it is not needed, validate it if it is.
  6.  
  7.  
  8. # ------------------------------------------------------------
  9. #  Unset insurance field if it is not required
  10. # ------------------------------------------------------------
  11.  
  12. add_filter("gform_pre_render_3", "sd_unset_insured");
  13.  
  14. function sd_unset_insured($form){
  15.     // the label of the insurance field
  16.     $insuredfield = array('Insured');
  17.     global $post;
  18.     // get post meta to check if insurance is required
  19.     $needsinsurance = get_post_meta($post->ID,'_sd_product_insurance',true);
  20.     // If value is 0, not required so unset
  21.     if ($needsinsurance==0) {
  22.         foreach($form['fields'] as $key=>$field) {
  23.             if(in_array($field['label'], $insuredfield)) {
  24.                 unset($form['fields'][$key]);
  25.             }
  26.         }
  27.     }
  28.     return $form;
  29. }
  30.  
  31. # ------------------------------------------------------------
  32. # Validate insurance field if it is required
  33. # ------------------------------------------------------------
  34.  
  35.  
  36. add_filter('gform_validation_3', 'sd_require_insured');
  37. function sd_require_insured ($validation_result){
  38.     // the relevant field by form id and field id
  39.     $insured = array("3:9");
  40.     $form = $validation_result["form"];
  41.     global $post;
  42.     // get post meta to check if insurance is required
  43.     $needsinsurance = get_post_meta($post->ID,'_sd_product_insurance',true);
  44.     // If value is 1, required so validate
  45.     if($needsinsurance==1) {
  46.     foreach($form['fields'] as &$field){
  47.         // find out if the field exists
  48.         if(in_array($form["id"] . ":" . $field['id'], $insured)){
  49.             $insuredinput= RGForms::post("input_{$field["id"] }");
  50.            
  51.              // if no confirmation is indicated, return an error          
  52.             if(!isset ($insuredinput) || $insuredinput!=1) {
  53.                 $field['failed_validation'] = true;
  54.                 $field['validation_message'] = 'You must confirm that you are insured';
  55.                 $validation_result['is_valid'] = false;
  56.                 $validation_result['form'] = $form;
  57.             }
  58.      
  59.         }
  60.     }
  61.         }
  62.     return $validation_result;
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement