Skip Navigation

[Gelöst] Taxonomies required – Multiple form ID’s in the same function

Dieser Thread wurde gelöst. Hier ist eine Beschreibung des Problems und der Lösung.

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 vor 6 Jahren, 7 Monaten. 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.

Heute stehen keine Supporter zur Arbeit im Werkzeugsatz-Forum zur Verfügung. Sie können gern Tickets erstellen, die wir bearbeiten werden, sobald wir online sind. Vielen Dank für Ihr Verständnis.

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)

Dieses Thema enthält 24 Antworten, hat 3 Stimmen.

Zuletzt aktualisiert von leilaG vor 6 Jahren, 6 Monaten.

Assistiert von: Minesh.

Author
Artikel
#810781

We would like to make the taxonomies a required field for multiple forms. We are currently using the below code for 1 form, how do we tweak it to incorporate other forms and their taxonomies?

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'] == 17097 ) {

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

return array($fields,$errors);
}
add_filter( 'cred_form_validate', 'tssupp_require_sector', 10, 2 );

#811083

Minesh
Supporter

Sprachen: Englisch (English )

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

Hello. Thank you for contacting the Toolset support.

Well - if you have same validation for all the forms you are using than you should try following code:

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 ,9999,222,111);
           if (in_array($form_data['id'], $form_ids)) {
                           /// your code goes here
             }

         return array($fields,$errors);

}

Where:
- Adjust the $form_ids array values with your original form ID.

If you have different validation for different forms then you can add the validation hook that targets the specific form id. In following example - the validation hook will target the CRED form ID 5 and 10 with separate hook dedicated to specific form ID.

add_filter('cred_form_validate_5', 'validate_form_5',10,2);
function validate_form_5( $data ) {
    // your code goes here	
}

add_filter('cred_form_validate_10', 'validate_form_10',10,2);
function validate_form_10( $data ) {
    // your code goes here	
}

More info:
=> https://toolset.com/documentation/programmer-reference/cred-api/#cred_form_validate

#854734

Thank you for the code, how to add the taxonomy code into this code?

#855322

Minesh
Supporter

Sprachen: Englisch (English )

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

Well - I just shared the function wrappers you need to use to check multiple form IDs.

The other code should be the same as per your original code that is within the if condition, so no change to check taxonomy condition.

For example:

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 ,9999,222,111);  // change form IDs here
           if (in_array($form_data['id'], $form_ids)) {

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


             }
 
         return array($fields,$errors);
 
}
#857196

I added the code, but none of the required fields are working in all forms. Code below -

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

#857271

Minesh
Supporter

Sprachen: Englisch (English )

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

Could you please share problem URL and access details. I will check this tomorrow first thing.

*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin and FTP) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.

I would additionally need your permission to de- and re-activate Plugins and the Theme, and to change configurations on the site. This is also a reason the backup is really important. If you agree to this, please use the form fields I have enabled below to provide temporary access details (wp-admin and FTP).

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

#867633

Minesh
Supporter

Sprachen: Englisch (English )

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

Well - what is the staging site URL - could you please confirm, I just wanted to make sure that I should not touch your live site.

Additionally, please share problem URL where you added the CRED form which you want to validate as well as which taxonomy fields you want to validate for which forms. Do you have different validations for each form?

#867801

versteckter Link

Forms -

versteckter Link
-Location
-Company Type
-Sectors: Contract Research Services, Environmental, Medtech, Professional Services, Therapeutics, Wellness (At least one of the sectors)

versteckter Link
-Location
-Job category

#867822

Minesh
Supporter

Sprachen: Englisch (English )

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

Thanks and I see you need to share staging site FTP/SFTP access details as well. Could you please share it.

#868072

Minesh
Supporter

Sprachen: Englisch (English )

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

Well - using the FTP access details, yes, its working but are you sure you shared the STAGING site FTP access details, as when I make any change and upload the file, it does not have any effect on staging site page.

Could you please confirm you send me FTP access details for staging site. Please check once and send me exact staging site FTP access details.

#872628

Minesh
Supporter

Sprachen: Englisch (English )

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

Ok - I've added following code to your current theme's functions.php file:

As you may notice - this hook is for validating CRED form with Id 17097.

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 can add same hook for your another form (adjust your CRED form Id) as well as adjust the validations you need.

For this form:
versteckter Link
-Location
-Job category

#876707

Minesh
Supporter

Sprachen: Englisch (English )

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

Could you please confirm that the solution I shared works for you and mark this ticket resolved 🙂

#876859

I have added the code but it is not working.

I am on holiday for one week from today 11/05/2018 at 17:00 UK time and will be back on the 21st May.

#881968

Minesh
Supporter

Sprachen: Englisch (English )

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

Ok - fine. Please get back to me when you back from vacation.

#901550

Hello, just wanted to let you know that I am back, is there anything we need to try to make this work.