Skip to content

Instantly share code, notes, and snippets.

@BronsonQuick
Created March 26, 2012 05:59
Show Gist options
  • Save BronsonQuick/2203307 to your computer and use it in GitHub Desktop.
Save BronsonQuick/2203307 to your computer and use it in GitHub Desktop.
Add a Gravity Forms validation filter to check default values
<?php
// Append the Gravity Forms ID to the end of the filter so that the validation only runs on this form
add_filter('gform_validation_1', 'custom_validation');
function custom_validation($validation_result){
$form = $validation_result["form"];
foreach($form['fields'] as &$field){
/* Check that the value of the field that was submitted. e.g. the name="input_1" that is generated by Gravity Forms */
if($_POST['input_1'] == "Your First Name"){
// set the form validation to false
$validation_result["is_valid"] = false;
//The field ID can be found by hovering over the field in the backend of WordPress
if($field["id"] == "1"){
$field["failed_validation"] = true;
$field["validation_message"] = "This field needs to be your actual first name.";
}
}
if ($_POST['input_2'] == "Company Name"){
// set the form validation to false
$validation_result["is_valid"] = false;
//The field ID can be found by hovering over the field in the backend of WordPress
if($field["id"] == "2"){
$field["failed_validation"] = true;
$field["validation_message"] = "This field needs to be your company name";
}
}
}
//Assign modified $form object back to the validation result
$validation_result["form"] = $form;
return $validation_result;
}?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment