Gravity Forms Field Validation

, ,

So the basic way to add validation functionality to a form is by using the gform_validation filter.  This gives you the ability to look at the value entered into any field and then allow it to be submitted or display an error message.

//runs validation on all forms
add_filter( 'gform_field_validation', 'your_function_name', 10, 4 );

//runs validation on only on form with specified ID
add_filter( 'gform_field_validation_XX', 'your_function_name', 10, 4 );

//runs validation on only on form with specified ID (XX) and field ID (YY)
add_filter( 'gform_field_validation_XX_YY', 'your_function_name', 10, 4 );

Being able to then check whether the value is an integer or check the formatting of what it should be. Here is an example where it is checking to see if the value is an integer.

// Checks Field ID 6 in Form ID 1 to see if it is an integer.
add_filter( 'gform_field_validation_1_6', 'custom_validation', 10, 4 );
function custom_validation( $result, $value, $form, $field ) {
 
    if ( $result['is_valid'] && !is_int( $value ) ) {
        $result['is_valid'] = false;
        $result['message'] = 'This field can only contain an integer.';
    }
    return $result;
}

 

Skills

Posted on

May 16, 2018

Submit a Comment

Your email address will not be published. Required fields are marked *