In a CRED form, you may want to require the User to select at least one checkbox in a checkboxes group custom field. It’s not possible to set up this requirement in wp-admin, but it’s possible to enforce it with the cred_form_validate API hook. Add the following code to your child theme’s functions.php file:
add_filter('cred_form_validate','at_least_one_checkbox_validation', 10, 2); function at_least_one_checkbox_validation( $field_data, $form_data ) { // field data are field values and errors list($fields,$errors)=$field_data; // forms is a comma-separated list of form IDs where this validation should be applied $forms = array( 12345,67890 ); // validate if the correct CRED form ID if ( in_array( $form_data['id'], $forms ) ) { if ( !isset($fields['wpcf-cbs']['value'][0])) $errors['cbs'] = 'At least one checkbox is required'; } return array( $fields, $errors ); }
Replace 12345,67890 with a comma-separated list of CRED form IDs where you want to apply this validation. Replace ‘cbs’ in both places to match your checkboxes field slug. For example if your checkboxes field slug is “features” then your code should be as follows:
if ( !isset($fields['wpcf-features']['value'][0])) $errors['features'] = 'At least one checkbox is required';
See the documentation for the cred_form_validate API hook for more information.
Let us know if this snippet is not working for you:
This snippet doesn’t work