Hi - I’m a long time developer but new to Gravity Forms and Wordpress. I can’t seem to get the filter gform_field_validation to fire. The bit of code is in the Functions.php file with the location of “wp-content/themes//resources/functions.php”.
To call the function that I created I’m using this:
//The following declaration targets field 1 in form 6
add_filter( 'gform_field_validation_6_1', 'your_function_name', 10, 4 );
That I copied from the documentation. Then changing it to use the values in my form (which is just generic code that I’m using to figure out how this works):
//Target field 24 in form 4
add_filter( 'gform_field_validation_24_4', 'my_custom_validation', 10, 4 );
function custom_validation( $result, $value, $form, $field ) {
if ( $result['is_valid'] && intval( $value ) > 10 ) {
$result['is_valid'] = false;
$result['message'] = 'Please enter a value less than 10';
}
return $result;
}
Also I can’t seem to figure out what the parameters 10 and 4 (sometimes I see 10 and 7) in the add_ filter call. In the docs it states that those are the form and field object. I’ve tried it with 10, 4 - 10,7 - 4, 24 - and leaving it out all together.
Thank you for the quick reply and the information!
I need to validate that the user picks a date from a date picker that’s at least one year in the past from the current date. Here’s my code:
add_filter( ‘gform_field_validation_4_24’,‘gt_one_year’ , 10, 4 );
function gt_one_year( $result, $value, $form, $field ) {
$userDate = rgpost('input_4_24');
$userYear = $userDate[2];
$currentYear = date('Y');
if ($currentYear == $userYear) {
$result['is_valid'] = false;
$result['message'] = "Date selected must be at least one year ago from today. Please correct.";
}
return $result;
}
To add the script to your form, you can add an HTML field to the form, and add the script there. Be sure you use the opening <script> and closing </script> tags.
As for your PHP code, I did not test it to know where it might not be working