Skip to content

Instantly share code, notes, and snippets.

@mandiwise
Last active August 29, 2015 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mandiwise/666b6576fe5ffda86261 to your computer and use it in GitHub Desktop.
Save mandiwise/666b6576fe5ffda86261 to your computer and use it in GitHub Desktop.
Checking to see if the field contained "Vancouver" and failing validation if it does.
function my_custom_validation( $validation_result ) {
$form = $validation_result["form"];
// Sorry, you don't get to live in Vancouver...
if ( $_POST['input_1'] == 'Vancouver' ) {
$validation_result["is_valid"] = false;
foreach ( $form["fields"] as &$field ) {
// NOTE: Replace 1 with the field you would like to validate
if ( $field["id"] == "1" ) {
$field["failed_validation"] = true;
$field["validation_message"] = "Sorry, you can't enter Vancouver here!";
break;
}
}
}
// Assign modified $form object back to the validation result
$validation_result["form"] = $form;
return $validation_result;
}
add_filter( 'gform_validation_[your form ID]', 'my_custom_validation' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment