Help debugging call to gform_field_validation

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.

Am I doing any of this correctly? Please help…

10 is the priority at which the filter runs.

4 is the number of arguments the function accepts.

Neither of those need to be to changed. Those are WordPress conventions and not related to Gravity Forms:

https://developer.wordpress.org/reference/functions/add_filter/

Now, what do you need help validating specifically? Let us know what your validation rules should be, and we’ll see if we can help. Thanks!

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;
}

How about if you only allowed them to pick dates prior to one year ago? You could use this filter for that:

If that’s an option, we can help with the validation code.

That would work great. I think that’s a better solution. Where do I place javascript in the form?

Just for my future understanding, do you see anything wrong with my php code?

I greatly appreciate your help!!

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 :slight_smile: