gform_validation

Description

The “gform_validation” filter allows custom validation logic to be used by Gravity Forms when a form is submitted.

Usage

Applies to all forms.

add_filter( 'gform_validation', 'custom_validation', 10, 2 );

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

add_filter( 'gform_validation_6', 'custom_validation', 10, 2 );
Note: DO NOT use the form specific version of this filter with forms that also have payment add-on feeds. If you need the validation to occur before the payment add-on validation use the generic version of the filter above and perform the form id check within the hooked function. Check the example below.

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.
  • $context string The context for the current submission. Since Gravity Forms 2.6.3.2. Possible values: form-submit, api-submit, or api-validate.

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!';
				// Assuming both inputs of your name field failed the validation.
				$field->set_input_validation_state( 1, false ); // Only for Gravity Forms 2.5.10 or higher.
				$field->set_input_validation_state( 3, false ); // Only for Gravity Forms 2.5.10 or higher.
				break;
			}
		}
	}

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

	return $validation_result;
}

3. Abort Submission if Total is Zero

This example will prevent the form submission if the Total field (id 3) is equal to zero.

add_action( 'gform_validation', function( $validation_result ) {
	GFCommon::log_debug( __METHOD__ . '(): running Total validation.' );
	$form = $validation_result['form'];

	// Return without changes if form id is not 10.
	if ( 10 != $form['id'] ) {
		return $validation_result;
	}

	// Change 3 to the id number of your Total field.
	if ( '$0.00' === rgpost( 'input_3' ) ) {
		GFCommon::log_debug( __METHOD__ . '(): Total field is empty.' );
		// Set the form validation to false.
		$validation_result['is_valid'] = false;
		// Find Total field, set failed validation and message.
		foreach ( $form['fields'] as &$field ) {
			if ( $field->type == 'total' ) {
				$field->failed_validation = true;
				$field->validation_message = 'You must select at least one product!';
				break;
			}
		}
	}

	// Assign modified $form object back to the validation result.
	$validation_result['form'] = $form;
	return $validation_result;
} );

3. Check the form id using the generic filter

This example shows how to check the form id when using the generic version of the filter.

add_filter( 'gform_validation', 'custom_validation' );
function custom_validation( $validation_result ) {
	$form = $validation_result['form'];
	// Return without changes if form id is not 10.
	if ( 10 != $form['id'] ){
		return $validation_result;
	}

	// Your custom code here.

	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.