Skip to content

Instantly share code, notes, and snippets.

@sirchrispy
Forked from getdave/gist:4578295
Created February 22, 2018 21:56
Show Gist options
  • Save sirchrispy/46078436df5918070736935a88741c51 to your computer and use it in GitHub Desktop.
Save sirchrispy/46078436df5918070736935a88741c51 to your computer and use it in GitHub Desktop.
Gravity Forms - custom field validation function for checkboxes. Ensures all checkboxes have been checked.
// Replace 7 with the ID of your form and 13 with the ID of the field you want to force "all required"
// http://www.gravityhelp.com/documentation/page/Gform_field_validation
add_filter("gform_field_validation_7_13", 'validate_tcs', 10, 4);
function validate_tcs($result, $value, $form, $field) {
// Convert the checkbox input name value (returned as part of "field")
// into the "underscored" ID version which is found in the $_POST
foreach ($field['inputs'] as $input) {
$input_post_value = 'input_' . str_replace('.', '_', $input['id']);
// Validate the value
if ( !isset( $_POST[$input_post_value] ) ) {
$result["is_valid"] = false;
$result["message"] = "You must accept <em>all</em> of the Terms and Conditions";
}
}
return $result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment