Skip Navigation

[Resolved] Form validation – make taxonomy field required – bug?

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

Problem:

Add validation to taxonomy fields on post forms.

Solution:

You can try filter hook "cred_form_validate", for example:

https://toolset.com/forums/topic/form-validation-make-taxonomy-field-required-bug/#post-2340979

Relevant Documentation:

https://toolset.com/documentation/programmer-reference/cred-api/#cred_form_validate

This support ticket is created 2 years, 10 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/Hong_Kong (GMT+08:00)

This topic contains 5 replies, has 2 voices.

Last updated by heatherR-2 2 years, 10 months ago.

Assisted by: Luo Yang.

Author
Posts
#2339007

Hi -
We have a post form that has several taxonomy required fields. We have this snippet in place and active, but it doesn't seem to be firing. I've tried it in the custom code section as well as just in the child theme. I can see the log entry for "here2", but it doesn't seem like the lcd_require_taxonomy function gets called. Any thoughts on why and how to resolve?
Thanks.

<?php
/**
* Require Coaches taxonomies in the listing forms
*/

toolset_snippet_security_check() or die( 'Direct access is not allowed' );

/**
* Require Coaches taxonomies in the listing form
*/
function lcd_require_taxonomy($field_data, $form_data) {
@error_log('here1');
try {
$requireds = array( // Array of taxonomies to enforce a value for
array(
'form_id' => 2179,
'taxonomy' => 'coaching-format',
'msg' => 'Please choose at least one Coaching Format'
),
array(
'form_id' => 2179,
'taxonomy' => 'education',
'msg' => 'Please choose at least one Education'
),
array(
'form_id' => 2179,
'taxonomy' => 'language',
'msg' => 'Please choose at least one Language'
),
array(
'form_id' => 2179,
'taxonomy' => 'niche',
'msg' => 'Please choose at least one Niche'
)
);
// Split $field_data into separate $fields and $errors
list( $fields, $errors ) = $field_data;

foreach ($requireds as $required) {
if ( $form_data['id'] == $required['form_id'] ) {
if ( empty( $fields[$required['taxonomy']]['value'] ) ) {
$errors[$required['taxonomy']] = $required['msg'];
}
}
}

return array($fields, $errors);
} catch (\Exception $e) {
@error_log('fatal=' . $e->getMessage());
return array($fields, $errors);
}
}
@error_log('here2');
add_filter( 'cred_form_validate', 'lcd_require_taxonomy', 10, 2 );

#2339337

Hello,

Since it is a custom codes problem, please provide a test site with the same problem, also point out the problem page URL and form URL, I need to test and debug it in a live website, thanks

#2340955

Thanks for the details, I can login into your website, will update here if find anything

#2340979
required-tax.jpg

I have done below modifications in your website:
1) Edit child theme file "functions.php", comment out line 70 ~115 with:

/**
...
/**

You can not use two PHP function with same function name, it will conducts PHP errors

2) Edit Toolset-> Custom Code - "require-taxonomy-on-forms", as below:

/**
 * Require Coaches taxonomies in the listing form
 */
function lcd_require_taxonomy($field_data, $form_data) { 
    try {
        // Split $field_data into separate $fields and $errors
        list( $fields, $errors ) = $field_data;

        $requireds = array(  // Array of taxonomies to enforce a value for
            array(
                'form_id' => 2179,
                'taxonomy' => 'coaching-format',
                'msg' => 'Please choose at least one Coaching Format'
            ),
            array(
                'form_id' => 2179,
                'taxonomy' => 'education',
                'msg' => 'Please choose at least one Education'
            ),
            array(
                'form_id' => 2179,
                'taxonomy' => 'language',
                'msg' => 'Please choose at least one Language'
            ),
            array(
                'form_id' => 2179,
                'taxonomy' => 'niche',
                'msg' => 'Please choose at least one Niche'
            )
        );
 
        foreach ($requireds as $required) {
            if ( $form_data['id'] == $required['form_id'] ) {
                if ( empty( $fields[$required['taxonomy']]['value'] ) ) {
                    $errors[$required['taxonomy']] = $required['msg'];
                }
            }
        }

     } catch (\Exception $e) {
        @error_log('fatal=' . $e->getMessage());
    } 
    return array($fields, $errors);
}
add_filter( 'cred_form_validate', 'lcd_require_taxonomy', 10, 2 );

Test it in front-end:
hidden link

It works fine, see my screenshot required-tax.jpg

Please test again, check if it is fixed, thanks

#2342171
Screenshot from 2022-04-15 10-12-26.png

Hi -
Thank you for taking a look. Unfortunately, when I go to that form, I still do not see any validation happening (I do see your screenshot), but it doesn't seem to be showing when I test. Do you have any thoughts on why?
Thanks.
I clicked on submit and this is what I'm seeing (see screenshot).

#2342249

Hi - What I didn't realize was that the custom validation wouldn't get called if the basic validation rejected it. So, I had been going to load the form, then clicking on submit. None of the custom validation messages show because the internal validation is rejecting the submission first and I assume never calling the custom validation function.
All should be good now.
Thank you for your help!