Skip Navigation

[Résolu] Need help using cred_form_validate please

This support ticket is created Il y a 5 années et 8 mois. 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.

Aucun de nos assistants n'est disponible aujourd'hui sur le forum Jeu d'outils. Veuillez créer un ticket, et nous nous le traiterons dès notre prochaine connexion. Merci de votre compréhension.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9: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/Karachi (GMT+05:00)

Ce sujet contient 6 réponses, a 2 voix.

Dernière mise à jour par julieP Il y a 5 années et 8 mois.

Assisté par: Waqar.

Auteur
Publications
#1218252

I have required custom field type radio with options Yes or No. If the user selects Yes they are then required to answer two other questions (these are only displayed when yes is selected). To validate that the two additional fields have been completed, I've used this hook but it's not right somewhere .

Any ideas please?

add_filter('cred_form_validate','ts_validate_data_882_874_881',10,2);
function ts_validate_data_882_874_881 ( $field_data, $form_data ){

	$form_id = array( 882,874,881 ); //form IDs

        list($fields,$errors)=$field_data;
    
	if (in_array($form_data['id'], $form_id) ) {
        
        if ( ( $fields['wpcf-custom-field-1']['value'] = Yes ) || ( empty( $fields['wpcf-custom-field-2']['value'] )) ){
            $errors['custom-field-2']='Please enter a response';
        }
    
	if ( ( $fields['wpcf-custom-field-1']['value'] = Yes ) || ( empty( $fields['wpcf-custom-field-3']['value'] )) ){
            $errors['custom-field-3']='Please enter a response';
        }
    }
    
    return array($fields,$errors);
}
#1218437

Just realised I should be using && not || so I changed by code to this:-

add_filter('cred_form_validate','ts_validate_data_882_874_881',10,2);
function ts_validate_data_882_874_881 ( $field_data, $form_data ){
 
    $form_id = array( 882,874,881 ); //form IDs
 
        list($fields,$errors)=$field_data;
     
    if (in_array($form_data['id'], $form_id) ) {
         
        if ( ( $fields['wpcf-custom-field-1']['value'] = Yes ) && ( empty( $fields['wpcf-custom-field-2']['value'] )) ){
            $errors['custom-field-2']='Please enter a response';
        }
     
    if ( ( $fields['wpcf-custom-field-1']['value'] = Yes ) && ( empty( $fields['wpcf-custom-field-3']['value'] )) ){
            $errors['custom-field-3']='Please enter a response';
        }
    }
     
    return array($fields,$errors);
}

but there's no change:-

if option 'Yes' is selected for custom-field-1 and custom-field-2 and custom-field-3 are left empty, the form doesn't submit and the messages appear (which is expected/wanted) however if text IS entered into custom-field-2 and custom-field-3, the form still won't submit and the messages still appear.

Is my code wrong/missing something?

#1218449

Played around a bit more and tried:-

=='Yes' rather than ='Yes'
$errors['wpcf-custom-field-2'] rather than $errors['custom-field-2']
$errors['wpcf-custom-field-3'] rather than $errors['custom-field-3']
&& empty( $fields['wpcf-custom-field-2']['value'] ) rather than && ( empty( $fields['wpcf-custom-field-2']['value'] ))

still no change!

#1218721

Hi Julie,

Thank you for contacting us and I'll be happy to assist.

You've been progressing in the right direction, but can you please also make sure that in the settings of "wpcf-custom-field-1" custom field the value added in the "Custom field content" is indeed "Yes"?
( example screenshot: lien caché )

The exact value from the "Custom field content" column should be used and not the "Display text".

The rest of the code looks correct and in case the issue still persists, you're welcome to share temporary admin login details in reply to this message.
( your next reply will be private )

regards,
Waqar

#1219532

Hi Waqar

I can confirm the value saved to the database for custom-field-1 is either No or Yes. custom-field-2 and custom-field-3 are single line fields.

With all the variations I've tried, I'm not sure now which code you're saying is correct. I've seen multiple ways of writing form validation hooks and I'm totally confused as to the exact formats required. For example, I've seen some ticket stating that

$errors['custom-field-3']='Please enter a response';

should exclude the wpcf- prefix and others stating it should include it. Other formats are also unclear (should I be using = or ==, should Yes be enclosed in single apostrophes like this 'Yes' or without, ['value'] correct for the single line fields?)

I can't provide access to my test site as this is locked down. I might be able to sort out something whereby you can take a look at a the issue on another site but before we go down that route (and because so much is unclear about the way this code snippet should be written), would you be good enough please to first show me the correct way this snippet should be written - it makes sense to get this right before we do anything else.

Thank you!!

#1220182

Hi Julie,

The following example snippet should make this more clear.

Screenshots of how fields are set:

Radio type field: lien caché
Two simple single line fields: lien caché
Front-end form: lien caché

Working code snippet:


add_filter('cred_form_validate','my_validation',10,2);
function my_validation($error_fields, $form_data)
{
    //field data are field values and errors
    list($fields,$errors)=$error_fields;
    //uncomment this if you want to print the field values

    //validate if specific form
    if ($form_data['id']==123)
    {

        if ( ( $fields['wpcf-section-featured']['value'] == 'yes' ) && ( empty( $fields['wpcf-section-field-1']['value'] )) ){
            $errors['wpcf-section-field-1']='Please enter a response in Field 1';
        }

        if ( ( $fields['wpcf-section-featured']['value'] == 'yes' ) && ( empty( $fields['wpcf-section-field-2']['value'] )) ){
            $errors['section-field-2']='Please enter a response in Field 2';
        }
        
    }

    return array($fields,$errors);
}

Screenshots of errors on front-end, after the form submission ( if "yes" is selected and the other two fields are left empty ):
lien caché

Note:

The "wpcf-" prefix is mandatory when targeting field value through the "$fields" array, but not when setting an error, as shown in the above code snippet.

I hope this helps and for troubleshooting custom PHP code, I'll recommend trying the "WP PHP Console" plugin ( https://wordpress.org/plugins/wp-php-console/ ).

Guide: lien caché

regards,
Waqar

#1221713

Hi Waqar

Many thanks for this - I especially appreciate the links to the debugging tools.

I've tested the code on a new basic site and it works there. I've scanned ALL hooks for inconsistencies and also used the Toolset custom code snippet tool as a means to check for most obvious mistakes but nothing is coming up as the possible cause.

My next step is to test the items on the new basic test site in a multi site scenario and to use the debugging tools you've linked to on the test site where the code isn't working. Failing all of these things, I think I'll have no choice but to rebuild my test site piece by piece in an attempt to ascertain the cause (hoping that won't be necessary).

I think it makes sense to close this ticket now and if my further testing shows the issue seems to be related to Toolset, I'll open it again.

Many thanks again for your assistance.