PLEASE NOTE: These forums are no longer utilized and are provided as an archive for informational purposes only. All support issues will be handled via email using our support ticket system. For more detailed information on this change, please see this blog post.

Challenges with validating select fields /

  1. The following code works to validate a text field (input_25 = City) after checking the value of another that defines what mode the form is being used in (input_27 = City or Event). However trying to do the same thing for a drop-down that has states (Input_19) has not proven to be so kind.

    It will always trip the validation error on the drop-down version regardless of entry. I've tried it in non-enhanced and enhanced mode, with any combination of rgpost() and comparison of values. It is posting to a custom field on the backend when successful.

    <?php function validate_rideshare($validation_result){
     $form = $validation_result["form"];
    
     $rideshareFormType = strpos($form['cssClass'], 'idealien_rideshare_gf');
    
     if( $rideshareFormType === false || $validation_result["is_valid"] === false)
      return $validation_result;
    
     if( $_POST["input_27"] == 'City' ) {
    
       foreach($form['fields'] as $key=>&$field) {
    
       switch ($field['postCustomFieldName']) {
    
        case 'idealien_rideshare_destinationCity':
    
          //Destination City
          if( !$_POST["input_25"] ) {
           $validation_result['is_valid'] = false;
           $field['failed_validation'] = true;
           $field['validation_message'] = 'This field is required.';
          }
         break; 
    
         /* case 'idealien_rideshare_destinationStateProv':
    
          //Destination State / Prov
          //**CORE PROBLEM AREA I THINK**
          if( !rgpost("input_19") ) {
           $validation_result['is_valid'] = false;
           $field['failed_validation'] = true;
           $field['validation_message'] = 'This field is required.';
          }
         break; */
    
        }
       }
      }
    
         $validation_result["form"] = $form;
    
         return $validation_result;
     }
    
    ?>

    Reading the docs further it seems like Gform_field_validation would be a good candidate for this, but a couple things confused me about it. If showing an example based on that is easy(ier), that would be great.

    • How to get the non-validated field (input_27 == city) without doing a foreach loop on the $form value.
    Posted 12 years ago on Saturday February 25, 2012 | Permalink
  2. The following will work for custom validation of select fields, to check against ' ' (single space with quotes) and rgpost().

    if( rgpost("input_19") == ' ' ) {
    	$validation_result['is_valid'] = false;
    	$field['failed_validation'] = true;
    	$field['validation_message'] = 'This field is required.';
    }
    Posted 12 years ago on Saturday March 3, 2012 | Permalink