Skip Navigation

[Resolved] I need to make taxonomy fields required on the front end forms

This support ticket is created 2 years, 1 month 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 6.66 hours from now. Thank you for your understanding.

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

Supporter timezone: Asia/Hong_Kong (GMT+08:00)

This topic contains 1 reply, has 2 voices.

Last updated by Luo Yang 2 years, 1 month ago.

Assisted by: Luo Yang.

Author
Posts
#2462571
Screenshot 2022-09-21 at 23.07.21.png

Tell us what you are trying to do?
I'd like t make all taxonomy fields on front end forms not only required but also have a zero / "please select" value to begin with. If the user does not select a value then they are presented with a "This value is required" error. Currently all I can see is that the taxonomy fields are not required and have the first available value selected. This allows the user to submit the form with an incorrect value selected.

See attached image - Job Title is not required after submitting the form after submitting without other required fields filled in.

Is there any documentation that you are following?
The tutorials.

What is the link to your site?
hidden link

#2462699

Hello,

Given that making a taxonomy required isn't something that is possible with wordpress by default, you can use Form filter hook "cred_form_validate" to add the custom validation, for example:
Toolset->Settings->Custom Code, create and activate one item, with below codes:

function tssupp_require_category($field_data, $form_data) {
 
      // Split $field_data into separate $fields and $errors
 
    list( $fields,$errors ) = $field_data;
   
    // validate specific form
    $form_id = 12345;  // specific post form ID
    $taxonomy_slug  = 'my-tax-slug'; // specific taxonomy slug

    if ( $form_data['id'] == $form_id ) {
        // check at least one of required taxonomies set
 
        if ( empty( $fields[$taxonomy_slug]['value'] ) ) {
            $errors[$taxonomy_slug] = 'You must choose at least one term';
        }
    }
  
    return array($fields,$errors);
}
 
add_filter( 'cred_form_validate', 'tssupp_require_category', 10, 2 );

Please replace 12345 with your post form ID
replace my-tax-slug with your custom taxonomy slug

More help:
https://toolset.com/documentation/programmer-reference/adding-custom-code/using-toolset-to-add-custom-code/
https://toolset.com/documentation/programmer-reference/cred-api/#cred_form_validate