bookmark_bordergform_validation

Description

Use this filter to create custom validation logic.

Usage

Applies to all forms

add_filter( 'gform_validation', 'custom_validation' );

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

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

Examples

1. Validate a specific field

This example uses the gform_validation filter to prevent a specific number from being entered in a field. In most cases the gform_field_validation() filter may be the better choice when validating a specific field.

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 ( rgpost( '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;

}

2. Send entry data to third-party

This example demonstrates a simple approach to posting submitted entry data to a third party application. You can use the response to determine the form validation result.

add_filter( 'gform_validation', 'post_to_third_party' );
function post_to_third_party( $validation_result ) {

	$form  = $validation_result['form'];
	$entry = GFFormsModel::get_current_lead();

	$post_url = 'http://thirdparty.com';
	$body     = array(
		'first_name' => rgar( $entry, '1.3' ),
		'last_name'  => rgar( $entry, '1.6' ),
		'message'    => rgar( $entry, '3' ),
	);
	GFCommon::log_debug( 'gform_validation: body => ' . print_r( $body, true ) );

	$request  = new WP_Http();
	$response = $request->post( $post_url, array( 'body' => $body ) );
	GFCommon::log_debug( 'gform_validation: response => ' . print_r( $response, true ) );

	if ( /* check something in the $response */ ) {
		// validation failed
		$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;
}

Placement

This code should be placed in the functions.php file of your active theme.

Source Code

This filter is located in GFFormDisplay::validate() in form_display.php