Hi there
I have followed this thread - https://toolset.com/forums/topic/taxonomy-required - to make a single select taxonomy a required field in a form to create a new post but for some reason it is not working. Hoping you can take a look at my setup and see if I have something wrong...
In the form I am using this shortcode for the taxonomy field:
[cred_field field='topic' force_type='taxonomy' output='bootstrap' display='select' single_select='true']
Then in the JS box I am using this code to set the first option:
jQuery(function( $ ) {
var s = '<option value="">CHOOSE A TOPIC</option>';
var c = $("select[name='topic[]']").prepend(s);
$("option:first", c).attr('selected', 'selected');
} );
Then in functions.php I have added this validate code:
add_filter('cred_form_validate','topic_validation',10,2);
function topic_validation($field_data, $form_data) {
list($fields,$errors)=$field_data;
if ($form_data['id']==4618) {
if (empty($fields['topic']['value']))
$errors['topic']='Please select a topic';
}
return array($fields,$errors);
}
There is no response unfortunately... The user can still submit the form without selecting a 'topic'...
Do you think I have something wrong with this setup?
Thanks in advance!
Rita
Hi Rita,
Thank you for contacting us and I'd be happy to assist.
During testing on my website, I was able to make the PHP validation code work, after slight modification:
add_filter('cred_form_validate','topic_validation',10,2);
function topic_validation($field_data, $form_data) {
list($fields,$errors)=$field_data;
if ($form_data['id']==4618) {
if (empty($fields['topic']['value'][0]))
$errors['topic']='Please select a topic';
}
return array($fields,$errors);
}
Note: I've changed "$fields['topic']['value']" with "$fields['topic']['value'][0]".
regards,
Waqar
Thank you Waqar.
I never would have guessed that. Appreciated. It works perfectly.
Rita
My issue is resolved now. Thank you!