Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save gbissland/2bd3df9aafd2cadaee07f1a86e3f2c88 to your computer and use it in GitHub Desktop.
Save gbissland/2bd3df9aafd2cadaee07f1a86e3f2c88 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