How would I adjust that code to require each checkboxes field in a single form to require at least one checkbox? I have a form that has 7 of these checkboxes fields and I do not want to have to add seven blocks of code. Is there a way to omit the "target-field-slug" section?
The code shown in that link is an illustration which you can use as a starting point for any modifications required for your particular use case, and if you are uncomfortable writing and testing PHP you should probably hire a developer to do it for you.
Looking at it, my first attempt at adding the same requirement (at least one of the checkboxes of a checkboxes field must be checked) would be to create an array of the field slugs and then iterate over them, repeating the same test for each.
add_filter( 'cred_form_validate', 'tssnippet_at_least_one_checkbox_validation', 10, 2 );
function tssnippet_at_least_one_checkbox_validation( $field_data, $form_data ) {
$target_forms = array( 12345, 67890 ); // Edit IDs of forms to apply this to
$target_field_slugs = array( 'field1', 'field2', 'field3', 'field4', 'field5', 'field6', 'field7' ); // Edit slugs of checkboxes fields
if ( ! in_array( $form_data['id'], $target_forms ) ) {
return $field_data;
}
// split $field_data into separate arrays of $fields and $errors
list( $fields, $errors ) = $field_data;
foreach ($target_field_slugs as $key => $target_field_slug) {
if ( ! isset( $fields[ 'wpcf-' . $target_field_slug ]['value'][0] ) ) {
$errors[ $target_field_slug ] = 'At least one checkbox is required';
}
}
return array( $fields, $errors );
}
I haven't tested that. If it doesn't work as expected, I suggest you familiarise yourself with dumping variables to your error logs which is the easiest way to identify where the code is failing.
For example, send the $field_data variable to your debug.log with