Created
October 2, 2014 14:17
-
-
Save pkostadinov-2create/66b101636c6cc19e6a9c to your computer and use it in GitHub Desktop.
Gravity Forms Validation Fix
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Custom Gravity Forms validation - make sure the form isn't submitted with the default values | |
* Useful for when your Gravity Form is displaying default values instead of the field labels. | |
* | |
* In order to apply the function to a specif form, please append _1 ( _2 .. ) to the 'gform_validation' hook. | |
* Example (gform_validation_1) | |
* | |
* @return validation results | |
*/ | |
add_filter( 'gform_validation', 'crb_validate_gravity_default_values' ); | |
function crb_validate_gravity_default_values( $validation_result ) { | |
// Get the form object from the validation result | |
$form = $validation_result["form"]; | |
// Get the current page being validated | |
$current_page = rgpost( 'gform_source_page_number_' . $form['id'] ) ? rgpost( 'gform_source_page_number_' . $form['id'] ) : 1; | |
// Loop through the form fields | |
foreach( $form['fields'] as &$field ){ | |
$value_number = rgpost( "input_{$field['id']}" ); | |
// If there's a default value for the field, make sure the submitted value isn't the default value | |
if ( !empty( $field['defaultValue'] ) && $field['defaultValue'] === $value_number ) { | |
$is_valid = false; | |
} else { | |
$is_valid = true; | |
} | |
// If the field is valid we don't need to do anything, skip it | |
if( !$is_valid ) { | |
// The field failed validation, so first we'll need to fail the validation for the entire form | |
$validation_result['is_valid'] = false; | |
// Next we'll mark the specific field that failed and add a custom validation message | |
$field['failed_validation'] = true; | |
$field['validation_message'] = 'You can\'t submit the default value.'; | |
} | |
} | |
// Assign our modified $form object back to the validation result | |
$validation_result['form'] = $form; | |
// Return the validation result | |
return $validation_result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment