Skip Navigation

[Resolved] Make Post Form Taxonomy Field Required

This support ticket is created 2 years, 10 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Our next available supporter will start replying to tickets in about 1.37 hours from now. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 -
- 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 14:00 – 18:00 13:00 – 18:00 -

Supporter timezone: America/Jamaica (GMT-05:00)

This topic contains 6 replies, has 2 voices.

Last updated by chrisO-12 2 years, 10 months ago.

Assisted by: Shane.

Author
Posts
#2266735

Tell us what you are trying to do?

How do I make a taxonomy/category field required in a post form?

Is there any documentation that you are following?
https://toolset.com/forums/topic/make-post-category-field-required/
I am not a programmer, so i do not understand this solution. What are details on how to do this?

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.

Thanks for your support.

What is the link to your site?

under development.

#2266861

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Chris,

Thank you for getting in touch.

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.

Thanks,
Shane

#2266923

Shane,

Thank you for the prompt reply.

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?

Thanks,
Chris

#2266939

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?

#2268403

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Christ,

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.

Thanks,
Shane

#2268737

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Chris,

The code should now be working.

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.

Thanks,
Shane

#2268771

Shane,

Thank you for the prompt solution. This works great.

Regards,
Chris