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 );
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
Thanks for the details, I can login into your website, will update here if find anything
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
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).
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!