Gform validation

Description

Use this filter to create custom validation logic.

Usage

Applies to all forms

<?php
add_filter("gform_validation", "custom_validation");
?>

You can also specify this per form by adding the form id after the hook name.

<?php
add_filter("gform_validation_6", "custom_validation");
?>

For an exhaustive walk-through on using the gform_validation hook see Using the Gravity Forms "gform_validation" Hook

Parameters

$validation_result

(array) contains the validation result and the current Form Object

Examples

This example uses the gform_validation filter to prevent a specific number from being entered in a field.


<?php

add_filter('gform_validation', 'custom_validation');
function custom_validation($validation_result){
    $form = $validation_result["form"];

    //supposing we don't want input 1 to be a value of 86
    if($_POST['input_1'] == 86){

        // set the form validation to false
        $validation_result["is_valid"] = false;

        //finding Field with ID of 1 and marking it as failed validation
        foreach($form["fields"] as &$field){

            //NOTE: replace 1 with the field you would like to validate
            if($field["id"] == "1"){
                $field["failed_validation"] = true;
                $field["validation_message"] = "This field is invalid!";
                break;
            }
        }

    }

    //Assign modified $form object back to the validation result
    $validation_result["form"] = $form;
    return $validation_result;

}

?>

Source Code

This filter is located in form_display.php

Search the Documentation