The only issue is that the error message is attributed to a particular field, so I would set the error for the first of the taxonomy fields and make the message text more generic, e.g. "You must select at least one sector".
I've updated the sample code from the other thread to allow for testing multiple taxonomies and requiring that at least one of them is selected.
function tssupp_require_sector($field_data, $form_data) {
// Split $field_data into separate $fields and $errors
list( $fields,$errors ) = $field_data;
// validate specific form
if ( $form_data['id'] == 12 ) {
// check at least one of required taxonomies set
if ( empty( $fields['first_taxonomy']['value'] ) && empty( $fields['second_taxonomy']['value'] ) && empty( $fields['third_taxonomy']['value'] ) && empty( $fields['fourth_taxonomy']['value'] ) ) {
$errors['first_taxonomy'] = 'You must choose at least one sector';
}
}
return array($fields,$errors);
}
add_filter( 'cred_form_validate', 'tssupp_require_sector', 10, 2 );
You will need to edit the form id, and the slugs of the taxonomies. I have added four in this example; the format should be clear if you need more or fewer.
Add this code to your theme's functions.php file, or using a plugin such as Code Snippets.
Great thanks, that's working now, only issue is the error message appears at the top of the page, but the page doesn't scroll back up once you click submit. Is there a fix for that?
You can change where the error messages appear so that instead of appearing at the top of the form (which they do by default) they can be next to the Submit button, for example.
When you edit the CRED form you will notice the following shortcode which provides the container for the error messages. You can move that elsewhere in the form.
Regarding making a single taxonomy required, are you talking about a different taxonomy than the ones already covered above?
You would add the test for another taxonomy in the same function you already have, something like this:
function tssupp_require_sector($field_data, $form_data) {
// Split $field_data into separate $fields and $errors
list( $fields,$errors ) = $field_data;
// validate specific form
if ( $form_data['id'] == 12 ) {
// check at least one of required taxonomies set
if ( empty( $fields['first_taxonomy']['value'] ) && empty( $fields['second_taxonomy']['value'] ) && empty( $fields['third_taxonomy']['value'] ) && empty( $fields['fourth_taxonomy']['value'] ) ) {
$errors['first_taxonomy'] = 'You must choose at least one sector';
}
// also check for some other unrelated taxonomy
if ( empty( $fields['taxonomy_a']['value'] ) ) {
$errors['taxonomy_a'] = 'You must select a taxonomy a';
}
}
return array($fields,$errors);
}
add_filter( 'cred_form_validate', 'tssupp_require_sector', 10, 2 );