Generally, it seems that some simple tasks like this would be built into the basic functionality of Toolset, and would not require adding code. Like a simple validation checkbox on a taxonomy field to set it to be required, like it is on custom fields. I am not a programmer, so this may not be as simple as adding to the custom fields, although I have seen it in another custom fields plugin.
I really like Toolset, but it is a cumbersome process to dig in and find a task that requires code to be added to the plugin, and then ask for help on how to do it. Seems as though it is built more for developers.
I do appreciate the prompt replies when there is a concern.
Given that making a taxonomy required isn't something that is possible with wordpress by default, we will need to make use of our custom validation function to make the taxonomy required on your frontend form.
This can be done by using the code below. Add the following to your Toolset custom code settings at Toolset->Settings->Custom Code. Once you've done this please ensure that you've activated it as well as change the ID 123 to the ID of your form.
function tssupp_require_category($field_data, $form_data) {
// Split $field_data into separate $fields and $errors
list( $fields,$errors ) = $field_data;
// validate specific form
if ( $form_data['id'] == 123 ) {
// check at least one of required taxonomies set
if ( empty( $fields['first_taxonomy']['value'] ) ) {
$errors['first_taxonomy'] = 'You must choose at least one term';
}
}
return array($fields,$errors);
}
add_filter( 'cred_form_validate', 'tssupp_require_sector', 10, 2 );
Once you've done this replace 'first_taxonomy' with the slug of your custom taxonomy. This should make your taxonomy field required.
I entered the code into the Custom Code section and changed the form ID to 19852 and in two places changed 'first_taxonomy' to 'country' (taxonomy slug), and activated. I was able to submit the form on the front end with no taxonomy selected in country. Does the form ID need to be in quotes? Other ideas?
Also, how would I enter 2 or 3 more taxonomies on the same form?
I tried it with the form ID in quotes and it did not work.
Another thought - the Taxonomy 'country' has two entries, with slugs 'canada' and 'united-states'. Only canada and united-states can be selected. In the code that you provided, I am using the Taxonomy slug 'country', as this is the custom taxonomy created using Toolset . Am I doing this right?
Yes you're doing it correctly, however I need to check this further for you to see why the validation isn't working as intended.
Would you mind allowing me to have admin access to the website so that I can have a more detailed look at this for you ?
Please where applicable please provide me with a link to an example page where I can see the issue.
I've enabled the private fields for your next response.
The issue was that the hook was calling the incorrect function name. After reviewing the code I managed to correct it.
function tssupp_require_category($field_data, $form_data) {
// Split $field_data into separate $fields and $errors
list( $fields,$errors ) = $field_data;
// validate specific form
if ( $form_data['id'] == 19852 ) {
// check at least one of required taxonomies set
if ( empty( $fields['country']['value'] ) ) {
$errors['country'] = 'You must choose at least one country';
}
}
return array($fields,$errors);
}
add_filter( 'cred_form_validate', 'tssupp_require_category', 10, 2 );
The code has been tested and is working on your site.