I have two taxonomies on a CPT that I want to make mandatory on CRED form submission.
I am using the credt_form_validate hook to force mandatory fields but that does not seem to work for two taxonomies but it does work for the featured image field. What could I be missing?
Form view -- hidden link
I also have custom JS to make sure the taxonomy field is empty on form load - hidden link
As a workaround, I tried to make this work by enabling AJAX but the form simply bypasses the mandatory field restriction and assigns the first value in the taxonomy dropdown to the taxonomy field.
Without AJAX, it does not even assign a field and just submits the form ignoring the validation.
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'] == 5371 ) {
// check if project category taxonomy is set
if ( empty( $fields['project-category']['value'] ) ) {
$errors['project-category'] = 'You must choose a project type';
}
// check if skils have been selected
if ( empty( $fields['skill-category']['value'] ) ) {
$errors['skill-category'] = 'You must select a skill';
}
// check if an industry has been selected
if ( empty( $fields['industry-category']['value'] ) ) {
$errors['industry-category'] = 'You must select an industry';
}
// check if an excerpt has been selected
if ( empty( $fields['post_excerpt']['value'] ) ) {
$errors['post_excerpt'] = 'You must select a project excerpt';
}
// check if a featured image has been selected
if ( empty( $fields['_featured_image']['value'] ) ) {
$errors['_featured_image'] = 'You must select a featured image';
}
// check if prompt description has been selected
if ( empty( $fields['wpcf-prompt-description']['value'] ) ) {
$errors['wpcf-prompt-description'] = 'You must select a project prompt description';
}
return array($fields,$errors);
}
add_filter( 'cred_form_validate', 'tssupp_require_sector', 10, 2 );
Hello,
You can debug the PHP codes manually, for example:
var_dump($fields['project-category']['value']);
die();
Submit the form in frontend, check the result if it is empty.
More help:
hidden link
This is a disappointing response.
First of all, making taxonomy mandatory is a basic request and it should be part of the Toolset offer without coding. But, it is not. So I worked with the toolset team and they provided me this code. It was working fine for a while but now it does not for two taxonomies. I tried to figure it out but it does not.
Even the custom JS is provided by the Toolset team and it all worked well but I am not sure why it does not now. What I don't understand is - I have a live product and if things just stop working and I get a response from you telling me to fix the code toolset provided. That seems a bit unfair.
Also, I am on US time and it would be good if someone in the US or another closer time zone could handle the request.
First of all, it is a custom codes problem, that means this code can work only in your one specific website, it might not work if your website settings are changed.
My answer above is telling you how to debug the codes, according to our support policy, we don't provide custom codes support, and you need to learn how to debug the custom PHP codes, in case your website settings are changed.
I have enabled the private message box, if you need more assistance for it, please provide a test site with the same problem, also point out the problem page URLs and form URL, where I can edit your custom PHP codes.
Thanks for the details, I have done below modifications in your website:
Edit the custom codes snippet "create-prompt-mandatory-fields-and-other-post-types", replace line 21 from:
if ( empty( $fields['project-category']['value'] ) ) {
To:
if ( empty( $fields['project-category']['value'][0] ) ) {
Since the taxonomy field value is an array, you can check the first item of array value, please test again, check if it is fixed.
Luo,
Thanks for this. I tried to create another project prompt on the staging site without entering the project category or industry category but the post still got saved.
Here is the video - hidden link
Where can I see the URL mentioned in the video you provided above?
I have tried the URL you mentioned in the private message box:
https://toolset.com/forums/topic/unable-to-make-taxonomy-mandatory-even-with-suggested-code-in-php/#post-2145005
hidden link
It works fine, I can see the correct error message when submit the post form, see my screenshot validation5.JPG
I can confirm that the issue is resolved on the staging site. Thanks, Luo. Appreciate the support.
My issue is resolved now. Thank you!