Skip Navigation

[Resolved] Condition for Multiple Form

This support ticket is created 3 years, 4 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 1 reply, has 2 voices.

Last updated by Minesh 3 years, 3 months ago.

Assisted by: Minesh.

Author
Posts
#2163431

I need help with below mention cusotmer code, as what I have to do if I want to use this condition on muliple forms.

https://toolset.com/forums/topic/condition-on-required-field/#post-1903687

-----

<?php
/**
* New custom code snippet (replace this with snippet description).
*/

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

// Put the code of your snippet below this comment.

add_filter('cred_form_validate','func_validate_my_form',10,2);
function func_validate_my_form($error_fields, $form_data) {

//field data are field values and errors
list($fields,$errors)=$error_fields;

if ($form_data['id']==117) {

if (empty($fields['wpcf-phone-number']['value']) and empty($fields['wpcf-wpcf-whatsapp-numbe']['value']) and empty($fields['wpcf-e-mail']['value']) ) {

$errors['wpcf-phone-number']='Please Enter at least One Value from Phone Number, Whatsapp or Email';
}
}
//return result
return array($fields,$errors);

this code is working for 1 form but I have mulitple form so need your help.

#2163493

Minesh
Supporter

Languages: English (English )

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

Hello. Thank you for contacting the Toolset support.

To apply the same code to multiple form IDs, you should try to use the following code:

add_filter('cred_form_validate','func_validate_my_form',10,2);
function func_validate_my_form($error_fields, $form_data) {
  
   //field data are field values and errors
    list($fields,$errors)=$error_fields;
  
   $form_ids = array(117,118,119);
    if (in_array($form_data['id'], $form_ids)) {
         
        if (empty($fields['wpcf-phone-number']['value']) and empty($fields['wpcf-whatsapp-link']['value']) and empty($fields['wpcf-e-mail']['value']) ) {
          
            $errors['wpcf-phone-number']='Please enter value for at least Phone,Email, Whatsapp';
        }
    }
    //return result
    return array($fields,$errors);
}

Where you can adjust the form ID with the given array:

 $form_ids = array(117,118,119);