Skip Navigation

[Resolved] CRED Taxonomy (Location) Field – How to Make Required

This support ticket is created 7 years, 4 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
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10: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/Kolkata (GMT+05:30)

Author
Posts
#470047

I am trying to: Make a CRED field based on a taxonomy (location) "required".

How can I do this?

Thanks.

#470183

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+01:00)

CRED form validation is actually determined by Types, which only adds validation and requirement to custom fields, not to taxonomies.

You can however use the CRED API to write custom validation rules and error messages, as described in the documentation here: https://toolset.com/documentation/user-guides/cred-api/#cfv

If you are not comfortable with that and you only need to specify the taxonomy is required, you could add a small custom JS snippet to add the 'required' attribute to your field when the form loads and let the browser's built-in validation handle it.

If you have a custom taxonomy with the slug 'colour', for example, then your field when added to the form will probably have the name='colour[]' attribute (you can check that by inspecting the source in your browser console). In which case you could add something like the following to your form custom JS:

( function( $ ) {
	$( document ).ready( function(){
		$("[name='colour[]']").prop('required',true);
	});
})( jQuery );
#470339

I've tried using both methods and still no luck:

I inspected the field in the browser and got the field name and used the JQuery above but no luck.

The filter below prevents the form being submitted but after the field is filled out the form still does not submit. Also how do I display the error message for the field?

add_filter('cred_form_validate','my_validation',10,2);
function my_validation($field_data, $form_data)
{
//field data are field values and errors
list($fields,$errors)=$field_data;

//uncomment this if you want to print the field values
//print_r($fields);

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

//check if listing location exists
if (empty($fields['listing-location']['name']))
{
//set error message for listing location
$errors['listing-location'] = 'Missing location';
}
}

//return result
return array($fields,$errors);
}

Thanks.

#470489

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+01:00)

You need to check the field value rather than name, which requires one minor edit.

I tested this on a site myself so it should work assuming you have edited the form id and taxonomy slug ('location-listing') appropriately:

/** Custom validation for form 573 **/
add_filter('cred_form_validate','my_validation',10,2);
function my_validation( $field_data, $form_data ){

	//split field data into field values and errors
	list($fields,$errors)=$field_data;

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

		//check if listing location value set to something
		if ( empty( $fields['listing-location']['value'] ) ){
			//set error message for listing location
			$errors['listing-location'] = 'Missing location';
		}
	}
	//return combined result
	return array($fields,$errors);
}
#470611

Great. That does work. Thanks.

But how do I display the error message in the field?

#471332

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Nigel is on vacation. This is Minesh here and I'll take care of this ticket now and try to help you further. Hope this is OK.

You can not display error message beneath the field when you use CRED validation hook as all error are displayed with error box just above the form.

#471636

Hi Minesh,

I'm not seeing the error box at all?

#471699

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

1)
Could you please share problem URL?

2)
*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin and FTP) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.

I would additionally need your permission to de- and re-activate Plugins and the Theme, and to change configurations on the site. This is also a reason the backup is really important. If you agree to this, please use the form fields I have enabled below to provide temporary access details (wp-admin and FTP).

I have set the next reply to private which means only you and I have access to it.

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.