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.