Skip to content

Instantly share code, notes, and snippets.

@rochow
Last active October 12, 2020 22:03
Show Gist options
  • Save rochow/643b6fdd0623fb8c5e3f to your computer and use it in GitHub Desktop.
Save rochow/643b6fdd0623fb8c5e3f to your computer and use it in GitHub Desktop.
Gravity Forms - Validation (Default Value = Fail)
<?php
// Gravity Forms Validation. Rejects any required input where $_POST value = default input value (ya, placeholders didn't use to exist!)
add_filter( 'gform_validation', 'mr_custom_validation' );
function mr_custom_validation( $validation_result ){
$form = $validation_result['form'];
foreach( $form['fields'] as &$field ){
if( '1' == $field['isRequired'] && 'checkbox' != $field['type'] ) {
if( $field['defaultValue'] == $_POST[ 'input_' . $field['id'] ] ) {
$validation_result['is_valid'] = false;
$field['failed_validation'] = true;
$field['validation_message'] = "This field is invalid!";
}
}
}
$validation_result['form'] = $form;
return $validation_result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment