Skip Navigation

[Resolved] How to make taxonomies required fields

This support ticket is created 6 years, 7 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
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+00:00)

This topic contains 6 replies, has 2 voices.

Last updated by leilaG 6 years, 7 months ago.

Assisted by: Nigel.

Author
Posts
#652290

2. How to make taxonomy tick box fields required fields, I have tried - required="true"
There's not a way to do this from wp-admin - it requires custom code using the cred_form_validate API. We have documentation for that API here:
https://toolset.com/documentation/programmer-reference/cred-api/#cred_form_validate
Here's another ticket with some sample code:
https://toolset.com/forums/topic/required-field-for-taxonomy-needed/#post-296267
If you have specific questions about this process, feel free to open new tickets so we can discuss in detail.

Is it possible to have a group of separate taxonomies as a required field e.g. Select a sector hidden link

#652502

Nigel
Supporter

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

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

Hi Leila

The code in the thread you linked to demonstrates how to make a single taxonomy required.

You can modify the if statement so that it tests whether all of a series of taxonomies are empty, something like

if ( empty( $fields['taxonomy_a']['value'] ) && empty( $fields['taxonomy_b']['value'] ) && empty( $fields['taxonomy_c']['value'] )  )

The only issue is that the error message is attributed to a particular field, so I would set the error for the first of the taxonomy fields and make the message text more generic, e.g. "You must select at least one sector".

#658538

Okay great! where exactly do I put the code? and where do I update the error message?

#672324

Nigel
Supporter

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

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

Hi Leila

I've updated the sample code from the other thread to allow for testing multiple taxonomies and requiring that at least one of them is selected.

function tssupp_require_sector($field_data, $form_data) {

	// Split $field_data into separate $fields and $errors
	list( $fields,$errors ) = $field_data;
	 
	// validate specific form
	if ( $form_data['id'] == 12 ) {

		// check at least one of required taxonomies set
		if ( empty( $fields['first_taxonomy']['value'] ) && empty( $fields['second_taxonomy']['value'] ) && empty( $fields['third_taxonomy']['value'] ) && empty( $fields['fourth_taxonomy']['value']  ) ) {

			$errors['first_taxonomy'] = 'You must choose at least one sector';
		}
	}

	return array($fields,$errors);
}
add_filter( 'cred_form_validate', 'tssupp_require_sector', 10, 2 );

You will need to edit the form id, and the slugs of the taxonomies. I have added four in this example; the format should be clear if you need more or fewer.

Add this code to your theme's functions.php file, or using a plugin such as Code Snippets.

#680107

Great thanks, that's working now, only issue is the error message appears at the top of the page, but the page doesn't scroll back up once you click submit. Is there a fix for that?

Could you please send me the code to make a single taxonomy required, I tried the code in the link you sent previously, but that didn't work - https://toolset.com/forums/topic/required-field-for-taxonomy-needed/#post-296267

#686993

Nigel
Supporter

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

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

Hi Leila

You can change where the error messages appear so that instead of appearing at the top of the form (which they do by default) they can be next to the Submit button, for example.

When you edit the CRED form you will notice the following shortcode which provides the container for the error messages. You can move that elsewhere in the form.

[cred_field field='form_messages' value='' class='alert alert-warning']

Regarding making a single taxonomy required, are you talking about a different taxonomy than the ones already covered above?

You would add the test for another taxonomy in the same function you already have, something like this:

function tssupp_require_sector($field_data, $form_data) {
 
    // Split $field_data into separate $fields and $errors
    list( $fields,$errors ) = $field_data;
      
    // validate specific form
    if ( $form_data['id'] == 12 ) {
 
        // check at least one of required taxonomies set
        if ( empty( $fields['first_taxonomy']['value'] ) && empty( $fields['second_taxonomy']['value'] ) && empty( $fields['third_taxonomy']['value'] ) && empty( $fields['fourth_taxonomy']['value']  ) ) {
 
            $errors['first_taxonomy'] = 'You must choose at least one sector';
        }

        // also check for some other unrelated taxonomy
        if ( empty( $fields['taxonomy_a']['value'] ) ) {
 
            $errors['taxonomy_a'] = 'You must select a taxonomy a';
        }        

    }
 
    return array($fields,$errors);
}
add_filter( 'cred_form_validate', 'tssupp_require_sector', 10, 2 );
#692662

Exactly what we wanted, appreciate all your help and support!!