Skip Navigation

[Résolu] Can’t validate checkboxes

Ce fil est résolu. Voici une description du problème et la solution proposée.

Problem: I would like to require the User to select at least one checkbox from a checkboxes group in a CRED form.

Solution: There's no way to do this from wp-admin, but you can apply backend validation using the cred_form_validate hook that will require at least one checkbox.

add_filter('cred_form_validate','cred_form_validation', 2, 10);
function cred_form_validation( $field_data, $form_data ) {
    // field data are field values and errors
    list($fields,$errors)=$field_data;
    $forms = array( 1234 );
    // check to see if this is the correct form 
    if ( in_array( $form_data['id'], $forms ) ) {
       
      if ( !isset($fields['wpcf-checkboxes']['value'][0]))
        $errors['checkboxes'] = 'At least one checkbox is required';
    }
 
    return array( $fields, $errors );
}

Change 1234 to match the CRED form ID, and change 'checkboxes' in both places to match the field slug.

Relevant Documentation:
https://toolset.com/documentation/programmer-reference/cred-api/#cred_form_validate

This support ticket is created Il y a 6 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.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

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

Dernière mise à jour par Christian Cox Il y a 6 années et 8 mois.

Assisté par: Christian Cox.

Auteur
Publications
#640229

Hi I'm not sure if it's a bug or an oversight:

I am trying to mark a multiple checkboxes field, but there is no option to do so.
The field is a user field.

If it is not possible to do so within the types UI, please let me know how I can require that at least one checkbox be checked using CRED.

Thank you

Adrian

#640302

Hi, it's not possible through the GUI but it's possible using the cred_form_validate hook:

add_filter('cred_form_validate','cred_form_validation', 2, 10);
function cred_form_validation( $field_data, $form_data ) {
    // field data are field values and errors
    list($fields,$errors)=$field_data;
    $forms = array( 1234 );
    // check to see if this is the correct form 
    if ( in_array( $form_data['id'], $forms ) ) {
      
      if ( !isset($fields['wpcf-checkboxes']['value'][0]))
        $errors['checkboxes'] = 'At least one checkbox is required';
    }

    return array( $fields, $errors );
}

Change 1234 to match the CRED form ID, and change 'checkboxes' in both places to match the field slug. Documentation for this API can be found here: https://toolset.com/documentation/programmer-reference/cred-api/#cred_form_validate

#677100

Hi, Thank you for the reply and sorry for my late reply.
Does this validation take place after the user hits submit, or will it remind them they have to select at least one checkbox (saying this field is required) before they submit?

#679869

It's a backend validation, so the User must submit the form first.