Skip Navigation

[Resolved] Make a taxonomy required in a Post Form

This thread is resolved. Here is a description of the problem and solution.

Problem: I would like to require at least one taxonomy term is selected in a Form.

Solution: Use the cred_form_validate API to check the Form submission to confirm at least one term is selected.

add_filter('cred_form_validate','term_checkboxes_form_validation', 10, 2);
function term_checkboxes_form_validation( $field_data, $form_data ) {
    $tax = 'YOUR-TAXONOMY-SLUG';
    $forms = array( 12345 );
    $msg = 'At least one term is required.';
  
    // 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 ) ) {
if ( !isset($fields[$tax]['value']) || $fields[$tax]['value']=='')
        $errors[$tax] = $msg;
    }
    return array( $fields, $errors );
}

Relevant Documentation:
https://toolset.com/documentation/programmer-reference/cred-api/

This support ticket is created 5 years 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
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 7 replies, has 2 voices.

Last updated by Jaime 5 years ago.

Assisted by: Christian Cox.

Author
Posts
#1367799

Tell us what you are trying to do?

I have a "New post Form" in which I need to set a taxonomy field as required.
But I don't see how to do this.

In my "New Post Form", users have a Select2 to choose their taxonomies, in which I have a maximum number of selections, 3 in my case. This is working fine (here below support tickets where you helped me to achieve that).
https://toolset.com/forums/topic/category-not-save-values-in-edit-form/
https://toolset.com/forums/topic/make-parent-taxonomy-in-a-dropdown-non-selectable-2/

I didn't found a way to make the taxonomies required (if there is, please just tell me) and I guessed I could set a minimum number of selections in Select2 (as there is an option for a maximum), but it seems it is not possible.
https://stackoverflow.com/questions/35635118/setting-minimum-value-to-select-in-select2-plugin

I also tried this
https://toolset.com/forums/topic/how-to-make-a-taxonomy-and-image-upload-field-required/
but it seems not working, I'm not sure if it could be because of the Select2 or what

Can you help me?
🙂

What is the link to your site?
hidden link

#1367869

There's not a built-in way to make a taxonomy required from the front-end of a form. You can validate the Form's contents using the cred_form_validate hook as shown here: https://toolset.com/forums/topic/how-to-make-a-taxonomy-and-image-upload-field-required/

How the select2 modifies the field data I'm not sure about. Most likely the problem is here:

if ( !isset($fields[$tax]['value'][0]))

You could turn on server logs and use error_log to inspect the data model and determine why it's failing. Here's an example:

add_filter('cred_form_validate','term_checkboxes_form_validation', 10, 2);
function term_checkboxes_form_validation( $field_data, $form_data ) {
    $tax = 'YOUR-TAXONOMY-SLUG';
    $forms = array( 12345 );
    $msg = 'At least one term is required.';
 
    // 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 );
}

If you're not familiar with server logs I can show you how to activate them temporarily. Go in your wp-config.php file and look for

define('WP_DEBUG', false);

Change it to:

define('WP_DEBUG', true);

Then add these lines, just after the WP_DEBUG line:

define('WP_DEBUG_LOG', dirname(__FILE__) . '/error_log.txt');
define( 'WP_DEBUG_DISPLAY', false );
@ini_set( 'display_errors', 0 );

Submit the Form, and this will create an error_log.txt file in your site's root directory. Please send me its contents. Once that is done, you can revert the changes you made to wp-config.php and delete the log file.

#1367909

Sorry Christian.
I'm a little bit lost.

What I've done:
1) Paste the example in the Toolset > Settings > Code Snippets, changing the taxonomy name and the form ID, so it looks like this:

add_filter('cred_form_validate','term_checkboxes_form_validation', 10, 2);
function term_checkboxes_form_validation( $field_data, $form_data ) {
$tax = 'YOUR-TAXONOMY-SLUG';
$forms = array( 12345 );
$msg = 'At least one term is required.';

// 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 );
}

2) Changed the wp-config.php file, and it looks like this (the part we are insterested)

/**
* For developers: WordPress debugging mode.
*
* Change this to true to enable the display of notices during development.
* It is strongly recommended that plugin and theme developers use WP_DEBUG
* in their development environments.
*
* For information on other constants that can be used for debugging,
* visit the Codex.
*
* @link https://codex.wordpress.org/Debugging_in_WordPress
*/
define('WP_DEBUG', true);
define('WP_DEBUG_LOG', dirname(__FILE__) . '/error_log.txt');
define( 'WP_DEBUG_DISPLAY', false );
@ini_set( 'display_errors', 0 );
define('WP_DEBUG_LOG', false);
define('WP_DEBUG_DISPLAY', false);

3) I submit the form, but nothing happens. No Debug or error_log.txt file is generated.
What I'm doing bad?

Thanks Christian.

(If you need it, I can give you access to my testing site).

#1367947

It's probably best for me to log in to wp-admin and take a quick look. Can you provide login credentials in the private fields here?

#1367981

Okay for a single select field it is a bit simpler, and I have edited the "if" line from this:

if ( !isset($fields[$tax]['value'][0]))

To be this:

if ( !isset($fields[$tax]['value']))

Can you tell me where to find the Form on the front-end of the site so I can test the update?

#1367999

Hi Christian
This Form is inside a process of user registering, so maybe it's not easy for you to do it.
Anyway, the form is in this page
hidden link

I suggest you to try it in thid page, which has an exact Form, but you can access as admin and see it, here
hidden link

The Form related to this new page is the ID 777. So you can make the same changes in this 777 form and see if it works.

Thanks a lot Christian!
Awesome!
🙂

#1368003
Screen Shot 2019-10-23 at 3.15.50 PM.png

Okay I made this update:

if ( !isset($fields[$tax]['value']) || $fields[$tax]['value']=='')

That seems to catch the missing taxonomy field. However, no error message is displayed in the Form because the form_messages shortcode is missing:

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

If that shortcode is missing, no error message will be displayed for the empty taxonomy field. I have added it back so you can see how the validation system works. You can adjust the text message in the custom code snippet.

#1368015

My issue is resolved now. Thank you!
Christian is awesome!