Skip Navigation

[Resolved] make single select taxonomy a required field in post form

This support ticket is created 3 years, 3 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
- 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: Asia/Karachi (GMT+05:00)

This topic contains 3 replies, has 2 voices.

Last updated by Rita 3 years, 3 months ago.

Assisted by: Waqar.

Author
Posts
#2191565

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

#2192611

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

#2192807

Thank you Waqar.
I never would have guessed that. Appreciated. It works perfectly.
Rita

#2192809

My issue is resolved now. Thank you!