Skip Navigation

[Resolved] Taxonomies required – Multiple form ID’s in the same function

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

Problem:
Taxonomies required - Multiple form ID's in the same function

Solution:
You can use Toolset Form hook "cred_form_validate" in order to validate the taxonomy field.

You can find proposed solution, in this case, with the following reply:
https://toolset.com/forums/topic/taxonomies-required-multiple-form-ids-in-the-same-function/page/2/#post-910665

Relevant Documentation:

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
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10: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/Kolkata (GMT+05:30)

This topic contains 24 replies, has 3 voices.

Last updated by leilaG 6 years, 6 months ago.

Assisted by: Minesh.

Author
Posts
#901583

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Well - as I mentioned with the following replay:
=> https://toolset.com/forums/topic/taxonomies-required-multiple-form-ids-in-the-same-function/#post-872628

When I checked it used to work when you submit the form without selecting any taxonomy term. Could you please confirm where exactly you are having the issue and with which form?

#901608

It's not working on these form, the taxonomies are not showing required when submitting the form -
hidden link
hidden link

Code below has been used -

add_filter('cred_form_validate', 'validate_form',10,2);
function validate_form( $data ) {
// Split $field_data into separate $fields and $errors
list( $fields,$errors ) = $field_data;

$form_ids = array( 17097,17313 ); // change form IDs here
if (in_array($form_data['id'], $form_ids)) {

// check at least one of required taxonomies set
if ( empty( $fields['contract-research']['value'] ) && empty( $fields['environmental']['value'] ) && empty( $fields['medtech']['value'] ) && empty( $fields['professional-services']['value'] ) && empty( $fields['therapeutics']['value'] ) && empty( $fields['wellness']['value'] )) {

$errors['contract-research'] = 'Environmental, Medtech, Professional Services, Therapeutics, Wellness: You must choose at least one sector';
}

// also check for some other unrelated taxonomy
if ( empty( $fields['location']['value'] ) ) {

$errors['location'] = 'You must select a location';
}

// also check for some other unrelated taxonomy
if ( empty( $fields['company-type']['value'] ) ) {

$errors['company-type'] = 'You must select a company type';
}

// also check for some other unrelated taxonomy
if ( empty( $fields['company-type']['value'] ) ) {

$errors['job-category'] = 'You must select a company type';
}
}

return array($fields,$errors);
}

#902191

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Well - again, I added following code to your current theme's functions.php file:

add_filter('cred_form_validate_17097','validate_my_form_17097',10,2);
function validate_my_form_17097( $field_data, $form_data) {
    // Split $field_data into separate $fields and $errors
list( $fields,$errors ) = $field_data;
 
    
                // check at least one of required taxonomies set
                    if ( empty( $fields['contract-research_hierarchy']['value']) && empty( $fields['environmental_hierarchy']['value']) && empty( $fields['medtech_hierarchy']['value']) && empty( $fields['professional-services_hierarchy']['value']) && empty( $fields['therapeutics_hierarchy']['value']) && empty( $fields['wellness_hierarchy']['value'])) {
              
                        $errors['contract-research_hierarchy'] = 'Environmental, Medtech, Professional Services, Therapeutics, Wellness:  You must choose at least one sector';
                    }
 
                    if ( empty( $fields['location']['value']) ) {
               
                        $errors['location'] = 'You must select a location';
                    }
                    if ( empty( $fields['company-type']['value']) ) {
               
                        $errors['company-type'] = 'You must select a company type';
                    }
            
        return array($fields,$errors);
}

The above code is to validate the following form and I can see its working fine:
=> hidden link

Now, you should create another function to validate the CRED form with ID = 17313.
=> hidden link

For example:

add_filter('cred_form_validate_17313','validate_my_form_17313',10,2);
function validate_my_form_17313( $field_data, $form_data) {
    // Split $field_data into separate $fields and $errors
list( $fields,$errors ) = $field_data;
 
       /// ADD HERE YOUR VALIDATIONS TO VALIDATE THIS FORM JUST LIKE ABOVE HOOK    
            
        return array($fields,$errors);
}

#905668

Sorry I thought I had replied to this last week.

I updated the code and the most of it is working now, apart from the below, which is still not working, I have tested the form a few time to make sure -

if ( empty( $fields['contract-research_hierarchy']['value']) && empty( $fields['environmental_hierarchy']['value']) && empty( $fields['medtech_hierarchy']['value']) && empty( $fields['professional-services_hierarchy']['value']) && empty( $fields['therapeutics_hierarchy']['value']) && empty( $fields['wellness_hierarchy']['value']) ) {

$errors['contract-research_hierarchy'] = 'Environmental, Medtech, Professional Services, Therapeutics, Wellness: You must choose at least one sector';

______________________________________________________________________________________________________________________

Full Code -

add_filter('cred_form_validate_17097','validate_my_form_17097',10,2);
function validate_my_form_17097( $field_data, $form_data) {
// Split $field_data into separate $fields and $errors
list( $fields,$errors ) = $field_data;

// check at least one of required taxonomies set
if ( empty( $fields['contract-research_hierarchy']['value']) && empty( $fields['environmental_hierarchy']['value']) && empty( $fields['medtech_hierarchy']['value']) && empty( $fields['professional-services_hierarchy']['value']) && empty( $fields['therapeutics_hierarchy']['value']) && empty( $fields['wellness_hierarchy']['value']) ) {

$errors['contract-research_hierarchy'] = 'Environmental, Medtech, Professional Services, Therapeutics, Wellness: You must choose at least one sector';
}

if ( empty( $fields['location']['value']) ) {

$errors['location'] = 'You must select a location';
}
if ( empty( $fields['company-type']['value']) ) {

$errors['company-type'] = 'You must select a company type';
}

return array($fields,$errors);
}

add_filter('cred_form_validate_17313','validate_my_form_17313',10,2);
function validate_my_form_17313( $field_data, $form_data) {
// Split $field_data into separate $fields and $errors
list( $fields,$errors ) = $field_data;

// check at least one of required taxonomies set
if ( empty( $fields['location']['value']) ) {

$errors['location'] = 'You must select a location';
}
if ( empty( $fields['job-category']['value']) ) {

$errors['job-category'] = 'You must select a job category';
}

return array($fields,$errors);
}

#906710

Hello,

Minesh is on vacation, you will get the answer as soon as As soon as he gets back on Monday

#908858

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Well - we need to go step by step now. Its all become more confusing.

This is the form:
=> hidden link

For above form - following are validation rules:


add_filter('cred_form_validate_17097','validate_my_form_17097',10,2);
function validate_my_form_17097( $field_data, $form_data) {
    // Split $field_data into separate $fields and $errors
list( $fields,$errors ) = $field_data;
  
     
                // check at least one of required taxonomies set
                    if ( empty( $fields['contract-research_hierarchy']['value']) && empty( $fields['environmental_hierarchy']['value']) && empty( $fields['medtech_hierarchy']['value']) && empty( $fields['professional-services_hierarchy']['value']) && empty( $fields['therapeutics_hierarchy']['value']) && empty( $fields['wellness_hierarchy']['value'])) {
               
                        $errors['contract-research_hierarchy'] = 'Environmental, Medtech, Professional Services, Therapeutics, Wellness:  You must choose at least one sector';
                    }
  
                    if ( empty( $fields['location']['value']) ) {
                
                        $errors['location'] = 'You must select a location';
                    }
                    if ( empty( $fields['company-type']['value']) ) {
                
                        $errors['company-type'] = 'You must select a company type';
                    }
             
        return array($fields,$errors);
}

You mean to say with above form - following part of validation is not working?

if ( empty( $fields['contract-research_hierarchy']['value']) && empty( $fields['environmental_hierarchy']['value']) && empty( $fields['medtech_hierarchy']['value']) && empty( $fields['professional-services_hierarchy']['value']) && empty( $fields['therapeutics_hierarchy']['value']) && empty( $fields['wellness_hierarchy']['value'])) {
               
                        $errors['contract-research_hierarchy'] = 'Environmental, Medtech, Professional Services, Therapeutics, Wellness:  You must choose at least one sector';
                    }
#908915

Hey Minesh,

Yes that's correct, the below part of the validation is not working -

if ( empty( $fields['contract-research_hierarchy']['value']) && empty( $fields['environmental_hierarchy']['value']) && empty( $fields['medtech_hierarchy']['value']) && empty( $fields['professional-services_hierarchy']['value']) && empty( $fields['therapeutics_hierarchy']['value']) && empty( $fields['wellness_hierarchy']['value'])) {

$errors['contract-research_hierarchy'] = 'Environmental, Medtech, Professional Services, Therapeutics, Wellness: You must choose at least one sector';
}

#910630

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Well - to ensure to which site I should check the validation you said. Do you want me to check with live site or staging site.

If you want to make me to check on live site: hidden link

Could you please share FTP access details for it. As I see with each reply the FTP access is of staging site.

I have set the next reply to private which means only you and I have access to it.

#910665

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Could you please check now. I've updated the code in your current theme's functions.php file as given under:

add_filter('cred_form_validate_17097','validate_my_form_17097',10,2);
function validate_my_form_17097( $field_data, $form_data) {
    // Split $field_data into separate $fields and $errors
list( $fields,$errors ) = $field_data;
 
    
                // check at least one of required taxonomies set
                    if ( empty( $fields['contract-research']['value']) && 
					     empty( $fields['environmental']['value']) && 
						 empty( $fields['medtech']['value']) && 
						 empty( $fields['professional-services']['value']) && 
						 empty( $fields['therapeutics']['value']) && 
						 empty( $fields['wellness']['value']) 
						 
						 ) {
              
                        $errors['contract-research'] = 'Environmental, Medtech, Professional Services, Therapeutics, Wellness:  You must choose at least one sector';
                    }
 
                    if ( empty( $fields['location']['value']) ) {
               
                        $errors['location'] = 'You must select a location';
                    }
                    if ( empty( $fields['company-type']['value']) ) {
               
                        $errors['company-type'] = 'You must select a company type';
                    }
            
        return array($fields,$errors);
}

I see its working now. The reason it was not working because checkboxes field names were different on your staging and live site. I've corrected the code as shared above and I can see its working now.

Could you please confirm it works as your end as well.

#910670

Brilliant! It's working 🙂 Thanks for all your help and troubleshooting.