Skip Navigation

[Resolved] Force taxonomy to be required in CRED post form

This support ticket is created 4 years, 8 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
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

Author
Posts
#1538589

Tell us what you are trying to do?
Create a new Post form and ensure that the users have to choose taxonomy values

Is there any documentation that you are following?
I have searched the forum and googled but wasn't able to find any recent relevant posts.

Is there a similar example that we can see?
For example, we want taxonomy AVAILABLE DAYS in Post Form "New Job Ad Form" to be required when a user submits the Job Ad. I have changed the Ad to Expert Mode in the hope that we could use required='true' but that did not work, although the "New Job Ad Form" was able to be saved successfully containing this code.

What is the link to your site?
hidden link

#1539143

Hello, unfortunately there isn't a simple shortcode attribute that will help you make a taxonomy field required in a Form. Instead, you can use the Forms API and a bit of custom code to check the value of that taxonomy field, and return an error if it's empty. We have a validation API that is just for this type of form input validation: https://toolset.com/documentation/programmer-reference/cred-api/#cred_form_validate

Here's a similar ticket with some example code:
https://toolset.com/forums/topic/taxonomy-required/

function tssupp_require_available_days($field_data, $form_data) {
 
// Split $field_data into separate $fields and $errors
list( $fields,$errors ) = $field_data;
 
// validate specific form
if ( $form_data['id']==12345) {
 
// check at least one of required taxonomies set
if ( empty( $fields['avail-days']['value'] ) ) {
 
$errors['avail-days'] = 'You must choose at least one available day';
}
}
 
return array($fields,$errors);
}
add_filter( 'cred_form_validate', 'tssupp_require_available_days', 10, 2 );

You will replace 12345 with the numeric ID of this post Form. You can find that ID in the list at Toolset > Post Forms. You will replace avail-days with the slug of this taxonomy. You can find that in Toolset > Taxonomies, then click the name of this taxonomy. You can rewrite the error message as well. Let me know if you have questions about that.

#1541797

HI Christian

Thanks for that, I'll give it a whirl now. We have 9 taxonomies on the same form, 6 of which we would we would like to ensure are non-null on submission.

1) Can I check all 6 in one script, or do I have to repeat this entire code 6 times?
I am imagining I could just repeat the one snippet of code in between, here's my draft:

function validate_nanny_ad_submission($field_data, $form_data) {

// Split $field_data into separate $fields and $errors
list( $fields,$errors ) = $field_data;

// validate New Native Nanny Ad Form (ID: 921)
if ( $form_data['id']==921) {

// check at least one of Available Day taxononmy term set
if ( empty( $fields['available-days']['value'] ) ) {

$errors['available-days'] = 'Please select at least one value for AVAILABLE DAYS.';
}
}

// check at least one Native Language taxonomy term set
if ( empty( $fields['native-language']['value'] ) ) {

$errors['native-language'] = 'Please select at least one value for NATIVE LANGUAGES.';
}
}

// check at least one Time of Day taxonomy term set
if ( empty( $fields['time-of-day']['value'] ) ) {

$errors['time-of-day'] = 'Please select at least one value for TIME OF DAY.';
}
}

return array($fields,$errors);
}
add_filter( 'cred_form_validate', 'validate_nanny_ad_submission', 10, 2 );

2) How can I ensure that the messages "Please select at least one value for ......" are translatable? Does [wpml_string] work inside PHP code?

Thanks and best regards
Simon

#1541843

Hi Christian

One further question:

3) We have a New Native Nanny Ad Form (ID: 921) and an Edit Native Nanny Ad Form (ID: 923). Naturally the same required fields need to be ensured, if the user edits an existing form. Can all the validation be done in one script also, eg by adding the ID of the Edit form to this script, so that this validation process applies to the "New" and "Edit" forms?

Thanks and best regards
Simon

#1541899

Hi Christian

Quick update: I tested the code and it works fine with one taxonomy, but not with several as in my draft. Maybe that helps in your investigations.

Kind regard
Simon

#1543819
Screen Shot 2020-03-08 at 1.24.57 PM.png

1) Can I check all 6 in one script, or do I have to repeat this entire code 6 times?
Yes that should be fine if you use the proper syntax and structure.

2) How can I ensure that the messages "Please select at least one value for ......" are translatable? Does [wpml_string] work inside PHP code?
You should use the __ function in PHP to register these, or any other, strings for string translation:

$errors['time-of-day'] = __('Please select at least one value for TIME OF DAY.', 'your-custom-theme-domain');

3) We have a New Native Nanny Ad Form (ID: 921) and an Edit Native Nanny Ad Form (ID: 923). Naturally the same required fields need to be ensured, if the user edits an existing form. Can all the validation be done in one script also, eg by adding the ID of the Edit form to this script, so that this validation process applies to the "New" and "Edit" forms?
Yes correct, you would just update the conditional logic so that the validation code applies to both New and Edit forms.

Quick update: I tested the code and it works fine with one taxonomy, but not with several as in my draft.
It looks like an issue with opening and closing curly brackets. See the attachment here for more specifics. You have two closing curly brackets immediately after the AVAILABLE DAYS error message. Those effectively close the form ID "if" statement, and then jump down to return array. The rest of the code before return array($fields,$errors) is skipped. So the fix for this is to remove the duplicate closing curly brackets after the errors['available-days'] message, after the errors['native-language'] message, and after the errors['time-of-day'] message, and so on for all 6 taxonomies.

#1547149

Hi Christian

I will try these out today and get back to you. Sorry for the late reply.

Cheers
Simon

#1547551

No problem, I will stand by for your update.

#1547631

HI Christian

So I got the script to check all 6 required taxonomies as required. GREAT! 🙂

1) To get the script to do both New and Edit Forms at the same time, would this be the correct syntax, ie just adding ,923 to the array, like this?

if ( $form_data['id']==921,923) {

2) I tried your suggestion for the WMPL string, but still couldn't find them under WMPL String Translation. I did this (as an example for available-day taxonomy:

if ( empty( $fields['available-days']['value'] ) ) {

$errors['available-days'] = __('Please select at least one value for AVAILABLE DAYS.', 'function-validate-taxonomies-on-nanny-ad-submission');
}

I was hoping to find a domain under WPML called function-validate-taxonomies-on-nanny-ad-submission , but couldn't, even after scanning for new strings. Also tried searching for one of the strings, it wasn't found.

I feel I may have misinterpreted what you meant by your-custom-theme-domain in your suggestion:

$errors['time-of-day'] = __('Please select at least one value for TIME OF DAY.', 'your-custom-theme-domain');

Thanks and best regards
Simon

#1548469

So I got the script to check all 6 required taxonomies as required. GREAT!
Excellent!

1) To get the script to do both New and Edit Forms at the same time, would this be the correct syntax, ie just adding ,923 to the array, like this?
The code was not written to support an array, only a single number. Here is an update that will support a comma-separated list (an array). I added a variable $forms to hold the array, and I changed the "if" line. Nothing else changed:

// validate New Native Nanny Ad Form (ID: 921)
$forms = array( 921, 923 );
if ( in_array( $form_data['id'], $forms ) ) {

You can continue to add more form IDs to the array if necessary.

2) I tried your suggestion for the WMPL string, but still couldn't find them under WMPL String Translation.
There is usually one more step to get this string to appear in String Translation, and it depends on how you added your form validation code. If the form validation API code is in your theme's functions.php file, then you must scan your theme for strings. Go to WPML > Theme and Plugins localization and scan your theme for strings. This should add the string to String Translation under the domain "function-validate-taxonomies-on-nanny-ad-submission" as you mentioned before. If you added the form validation code in a plugin, you would use the same page to scan your plugin for strings.

#1548751

Hi Christian

2) Thanks for the code adjust! You're a star!

3) I'm just adding this code via Toolset > Settings > Custom Code as a code snippet.

I tried first of all scanning all Toolset plugins, without joy, then I scanned every plugin we are using and every theme (only have Avada and Twenty Twenty) and still can't get the strings to appear in WPML string translation.

Any ideas?

Cheers
Simon

#1548929

I see, a custom code snippet isn't included in those scans by WPML. You could use the WPML API wpml_register_single_string to register your string. Here is the documentation for that API: https://wpml.org/wpml-hook/wpml_register_single_string/

Here's an example of how to use it:

/**
 * Your custom snippet description
 */

toolset_snippet_security_check() or die( 'Direct access is not allowed' );

do_action( 'wpml_register_single_string', 'function-validate-taxonomies-on-nanny-ad-submission', 'tax-validation-on-nanny-ad-submission', 'Please select at least one value for AVAILABLE DAYS.' );

// the rest of your code snippet goes here

Let me find out if this is the best way to register your string in a Code Snippet. I will have to reach out to my WPML consultants. I'm closing for the day and I will return Sunday so hopefully I will have a final answer for you then.

#1551295

Hi Christian

OK, I will wait for your response on Sunday before I start implementing the other method.

Thanks and regards
Simon

#1551607
Screen Shot 2020-03-15 at 6.51.45 PM.png
Screen Shot 2020-03-15 at 6.52.24 PM.png

Okay after consulting with the WPML team an easier and more practical way to get those strings to register for String Translation is to turn on automatic string registration in WPML > String Translation. You can limit the auto-register to the custom text domain if you'd like. Leave that setting turned on while you test on the front-end of the site. Trigger the error message at least once, and the string should be automatically registered for you in String Translation. Don't forget to turn off the auto-registration feature when you're finished, otherwise it can slow down your site.

#1552099

Hi Christian

Thanks for that. It would appear to me that that wouldn't be a method we should support in a production environment due to performance considerations. It even states "* This feature is only intended for sites that are in development. It will significantly slow down the site, but help you find strings that WPML cannot detect in the PHP code".

Instead, I used your original suggestion and added the following to the code snippet validate_taxonomies_on_nanny_ad_submission, which would be simple since we only have 6 cases:
do_action( 'wpml_register_single_string', 'function-validate-taxonomies-on-nanny-ad-submission', 'tax-validation-on-nanny-ad-submission', 'Please select at least one value for TYPE OF WORK.' );
do_action( 'wpml_register_single_string', 'function-validate-taxonomies-on-nanny-ad-submission', 'tax-validation-on-nanny-ad-submission', 'Please select at least one value for AVAILABLE DAYS.' );
do_action( 'wpml_register_single_string', 'function-validate-taxonomies-on-nanny-ad-submission', 'tax-validation-on-nanny-ad-submission', 'Please select at least one value for TIME OF DAY.' );
do_action( 'wpml_register_single_string', 'function-validate-taxonomies-on-nanny-ad-submission', 'tax-validation-on-nanny-ad-submission', 'Please select at least one value for NATIVE LANGUAGES.' );
do_action( 'wpml_register_single_string', 'function-validate-taxonomies-on-nanny-ad-submission', 'tax-validation-on-nanny-ad-submission', 'Please select at least one value for CHILDRENS AGE RANGES.' );
do_action( 'wpml_register_single_string', 'function-validate-taxonomies-on-nanny-ad-submission', 'tax-validation-on-nanny-ad-submission', 'Please select at least one value for MODES OF TRANSPORT.' );

However, when I go to WPML > String Translation, I only see one string for translation under domain function-validate-taxonomies-on-nanny-ad-submission. "Please select at least one value for CHILDRENS AGE RANGES". I can't see any logic as to why it would have chosen only this one, since it is not the first or last in the list and alphabetically wouldn't be the first or last either. 🧐The same happened with the code snippet function-validate-taxonomies-on-job-ad-submission but in this case it chose only "Please select at least one value for MODES OF TRANSPORT".

What are the four arguments of the do_action? Perhaps I need to change 5 of them to something else, to register them correctly? Or is it supported to register multiple strings like this?

Thanks and regards
Simon