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
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.
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 );
}
It is 12345 that needs to be the form ID:
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 );
}