Gravity Forms Field Validation

Authored by

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

 

Other Code Snippets

Change Permissions or Ownership of Files or Directories Recursively

Code Snippet | Linux

Sometimes when you are moving files from one host to another, you have to change the files owner or permissions so the web server can utilize it properly.

Read More
Find Files by Size

Code Snippet | Linux

Here is a Linux command line that searches for files that are over a certain size so that you can find where you might be able to free up some space. Useful to find error_logs that have run amok.

Read More
Import MySQL from Command Line

Code Snippet | Linux | MySQL

Here is a method to import a SQL file into a database via the Linux command line.

Read More
linkedin facebook pinterest youtube rss twitter instagram facebook-blank rss-blank linkedin-blank pinterest youtube twitter instagram