gform_validation

gform_validation

DescriptionUsageParametersExamples1. Validate a specific field2. Send entry data to third-party3. Abort Submission if Total is ZeroPlacementSource Code

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' );

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

add_filter( 'gform_validation_6', 'custom_validation' );

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.

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

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_filter( 'gform_validation', 'gf_fail_for_total_zero' );
function gf_fail_for_total_zero( $validation_result ) {
GFCommon::log_debug( __METHOD__ . '(): running.' );
$form = $validation_result['form'];

// Change 3 to the id number of your Total field.
if ( empty( 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;
}

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.

發表回覆

您的郵箱地址不會被公開。 必填項已用 * 標註