Skip Navigation

[Resolved] I would like to make my taxonomy on post form required.

This support ticket is created 3 years 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
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: Africa/Casablanca (GMT+01:00)

This topic contains 3 replies, has 2 voices.

Last updated by Jamal 3 years ago.

Assisted by: Jamal.

Author
Posts
#2031415
Screen Shot 2021-04-23 at 6.45.18 PM.png

Tell us what you are trying to do?

I am trying to a mulit-select taxonomy field on my post form required. I tried required="true" attribute but it seems not working. It seems many other support posts say the same thing so what's the point of having this attribute?

Anyway, these are the form that contains the taxonomy (slug = business-category).

hidden link
hidden link

contains the following code:

<label>Category</label><span class="red-note">*</span>
[cred_field field='business-category' force_type='taxonomy' output='bootstrap' display='select']
[cred_field field="business-category_add_new" taxonomy="business-category" type="add_new"]
<br>

Please let me know how can I make this happen.

Is there any documentation that you are following?

https://toolset.com/forums/topic/required-fields-in-front-end-forms/

Is there a similar example that we can see?

What is the link to your site?

hidden link

#2032263

Jamal
Supporter

Languages: English (English ) French (Français )

Timezone: Africa/Casablanca (GMT+01:00)

Hello and thank you for contacting the Toolset support.

The required attribute is not meant for taxonomy fields, it is meant for custom fields. Taxonomies are by, WordPress design, not required. So, you will need custom code to make a taxonomy field required on a form. Check the custom code in this previous ticket https://toolset.com/forums/topic/make-a-taxonomy-required-in-a-post-form/

I hope this helps. Let me know if you have any questions.

#2033475

Hi Jamal,

I put the following code on functions.php (using snippets plugin)
I pub "business-category" for $tax and replaced id with the form ID.
It seems not working. Could you assist this?

add_filter('cred_form_validate','term_checkboxes_form_validation', 10, 2);
function term_checkboxes_form_validation( $field_data, $form_data ) {
$tax = 'business-category';
$forms = array( 12345 );
$msg = 'Choose at least one category.';

// you should not edit below this line

// field data are field values and errors
list($fields,$errors)=$field_data;
// validate if correct CRED form ID
if ( in_array( $form_data['8455'], $forms ) ) {
error_log('this is the correct form');
error_log(print_r($fields, true));
if ( !isset($fields[$tax]['value'][0]))
$errors[$tax] = $msg;
}
return array( $fields, $errors );
}

#2033909

Jamal
Supporter

Languages: English (English ) French (Français )

Timezone: Africa/Casablanca (GMT+01:00)

It is 12345 that needs to be the form ID:

$forms = array( 8455 );

If you want to use the code on another form(ID: 777), add it to the $froms array:

$forms = array( 8455, 777 );

So, the code should be:

add_filter('cred_form_validate','term_checkboxes_form_validation', 10, 2);
function term_checkboxes_form_validation( $field_data, $form_data ) {
    $tax = 'business-category';
    $forms = array( 8455 );
    $msg = 'Choose at least one category.';

    // you should not edit below this line

    // field data are field values and errors
    list($fields,$errors)=$field_data;
    // validate if correct CRED form ID
    if ( in_array( $form_data['id'], $forms ) ) {
        error_log('this is the correct form');
        error_log(print_r($fields, true));
        if ( !isset($fields[$tax]['value'][0]))
            $errors[$tax] = $msg;
    }
    return array( $fields, $errors );
}
This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.