Skip Navigation

[Resolved] How to make taxonomy field required and add default option to taxonomy dropdown on Toolset form

This thread is resolved. Here is a description of the problem and solution.

Problem:
How to make taxonomy field required and add default option to taxonomy dropdown on Toolset form

Solution:
You will have to add custom Javascript code to add default option for taxonomy dropdown as well as to make taxonomy field required.

You can find the proposed solution in this case with the following reply:
https://toolset.com/forums/topic/add-select-one-to-taxonomy-dropdown-in-cred-form/#post-2295083

Relevant Documentation:

This support ticket is created 3 years, 6 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.

Sun Mon Tue Wed Thu Fri Sat
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10: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/Kolkata (GMT+05:30)

This topic contains 7 replies, has 2 voices.

Last updated by karenZ 3 years, 6 months ago.

Assisted by: Minesh.

Author
Posts
#2292059
Screen Shot 2022-02-14 at 10.38.25 AM.png
Screen Shot 2022-02-14 at 10.34.44 AM.png

Hello,
We have several taxonomy fields in our CRED form, which currently show the first taxonomy selection automatically. This could cause issues if people forget to change it before submitting their form.

Can you please advise how to show "Select One" as the default in the taxonomy dropdown - instead of the first taxonomy option? Secondly, how can we set the Taxonomy Dropdown field to be required?

You can see an example in the attached screenshots.

On this form we have a "Parade City" form, which is automatically selecting "Baton Rouge" as the answer because it is the first city in the list. We need it to select NO option automatically and instead show text to "Select One". But also make the field required, so that one city must be selected in order to submit the form.

#2292505

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

To add the default option to your taxonomy dropdown, you will require to add the custom jQuery/Javascript code. Please check the following related ticket that may help you:
- https://toolset.com/forums/topic/drop-down-list-of-taxonomies-first-choice/

To make the taxonomy field required, you will again have to add custom JS code.

For example:
- The following code will add the default option "Select One" and add the required validation rule to the taxonomy "student-type". You can change the taxonomy slug where applicable with the following code.

jQuery(document).ready(function($){
  
jQuery("select[name='student-type[]']").prepend('<option value="">-Select One-</option>');
jQuery("select[name='student-type[]']")[0].selectedIndex = 0;
  
});

jQuery(document).on('cred_form_ready', function() {
      
  jQuery('[name="student-type[]"]').addClass('js-wpt-validate');
    
  jQuery.validator.addClassRules("js-wpt-validate", {
    required: true,
   });
    
});
#2293007
Screen Shot 2022-02-15 at 8.59.57 AM.png
Screen Shot 2022-02-15 at 8.57.41 AM.png

Hello,
I've added the JS code to my form, and the "Select One" is working correctly. However, the validation is causing an error.

Instead of making only that taxonomy field required, it is requiring several other fields to suddenly be required too. Even though those fields are not set to "Required". See attached screenshot and the multiple "This field is required" messages that are suddenly being added.

Can you confirm if there is any additional code I need to add or if my code is not written correctly?

#2294019

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Can you please try to use the following JS code:

jQuery(document).on('cred_form_ready', function() {
       
   jQuery('[name="city[]"]').rules( "add", {
  required: true
});

});

I hope this will fix the issue.

#2294425

I've updated the JS with the revised code you provided, see below my full code. But when I tested the form it did NOT validate the "City" field - the error message saying "This Field is Required" did not appear. It did resolve the previous issue of multiple fields being required. Can you advise if the code is correct?

jQuery(document).ready(function($){

jQuery("select[name='city[]']").prepend('<option value="">Select One</option>');
jQuery("select[name='city[]']")[0].selectedIndex = 0;

});

jQuery(document).on('cred_form_ready', function() {

jQuery('[name="city[]"]').rules( "add", {
required: true
});

});

#2294445

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Can you please try to delete the browser cache and check once again.

If it still does not work, please share problem URL and admin access details.

*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.

I have set the next reply to private which means only you and I have access to it.

#2295083

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

I've adjusted the code as given under:

jQuery(document).on('cred_form_ready', function() {
   jQuery('[name="city[]"]').addClass('js-wpt-validate');   
   jQuery('[name="city[]"]').rules( "add", {
  required: true
});
 
});

I can confirm its working as expected at this end. Can you please confirm it works at your end as well.

#2295855

My issue is resolved now. Thank you!