Skip Navigation

[Resolved] Require at least one taxonomy term to submit a Form

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

Problem: I would like to set up some custom validation for a post Form. I would like to require at least one term from a custom taxonomy.

Solution: Use the cred_form_validate API to test the value of the taxonomy field, and return an error if none is selected.

function tssupp_require_sector($field_data, $form_data) {

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

// validate specific form
if ( $form_data['id']==2454) {

// check at least one of required taxonomies set
if ( empty( $fields['p-carer-time']['value'] ) ) {

$errors['p-carer-time'] = 'You must choose at least one sector';
}
}

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

Relevant Documentation:
https://toolset.com/documentation/programmer-reference/cred-api/#cred_form_validate

This support ticket is created 5 years, 5 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)

This topic contains 5 replies, has 2 voices.

Last updated by Mario 5 years, 5 months ago.

Assisted by: Christian Cox.

Author
Posts
#1268037

Tell us what you are trying to do?

Taxonomy required in form.
I have used this code but it is not picking up the taxonomy.
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
function tssupp_require_sector($field_data, $form_data) {

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

// validate specific form
if ( $form_data['id']==2368) {

// check at least one of required taxonomies set
if ( empty( $fields['during-the-day']['value'] ) && empty( $fields['during-the-evening']['value'] ) && empty( $fields['over-night']['value'] ) ) {

$errors['during-the-day'] = 'You must choose at least one sector';
}
}

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

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

Is there any documentation that you are following?
https://toolset.com/forums/topic/how-to-make-taxonomies-required-fields/

#1268271
Screen Shot 2019-06-13 at 10.36.46 AM.png

Hi, are during-the-day, during-the-evening, and over-night different taxonomies, or different terms in the same taxonomy? Based on the names, I am guessing these are 3 terms in the same taxonomy. If I am guessing correctly, your code should be updated like this:

function tssupp_require_sector($field_data, $form_data) {

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

// validate specific form
if ( $form_data['id']==2368) {

// check at least one of required taxonomies set
if ( empty( $fields['schedule']['value'] ) ) {

$errors['schedule'] = 'You must choose at least one sector';
}
}

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

Replace schedule with the slug of the taxonomy that contains the 3 terms. If I have misunderstood, let me know and I'll take a closer look.

#1268451

Hi Christian,
Still not detecting the checkbox, I changed 'schedule' to 'p-carer-time'
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
function tssupp_require_sector($field_data, $form_data) {

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

// validate specific form
if ( $form_data['id']==2368) {

// check at least one of required taxonomies set
if ( empty( $fields['p-carer-time']['value'] ) ) {

$errors['p-carer-time'] = 'You must choose at least one sector';
}
}

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

#1268469

May I log in and take a look at this? Please let me know where I can find the Form on the front-end of the site.

#1268617

I think the Form ID is incorrect here:

if ( $form_data['id']==2368) {

Form 2368 is a User form, not a post form. I think this should be 2454, right?

#1268649

My issue is resolved now. Thank you!