Skip Navigation

[Resolved] Require at least one checkbox of a checkboxes field for all checkboxes fields.

This support ticket is created 4 years, 11 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
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+00:00)

This topic contains 2 replies, has 2 voices.

Last updated by Ronald 4 years, 10 months ago.

Assisted by: Nigel.

Author
Posts
#1474195

I found your tutorial here
https://toolset.com/documentation/adding-custom-code/require-at-least-one-checkbox-of-a-checkboxes-field-to-be-checked-in-a-toolset-form/

How would I adjust that code to require each checkboxes field in a single form to require at least one checkbox? I have a form that has 7 of these checkboxes fields and I do not want to have to add seven blocks of code. Is there a way to omit the "target-field-slug" section?

#1474851

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Hi there

The code shown in that link is an illustration which you can use as a starting point for any modifications required for your particular use case, and if you are uncomfortable writing and testing PHP you should probably hire a developer to do it for you.

Looking at it, my first attempt at adding the same requirement (at least one of the checkboxes of a checkboxes field must be checked) would be to create an array of the field slugs and then iterate over them, repeating the same test for each.

add_filter( 'cred_form_validate', 'tssnippet_at_least_one_checkbox_validation', 10, 2 );
function tssnippet_at_least_one_checkbox_validation( $field_data, $form_data ) {
 
    $target_forms = array( 12345, 67890 ); // Edit IDs of forms to apply this to
    $target_field_slugs = array( 'field1', 'field2', 'field3', 'field4', 'field5', 'field6', 'field7' ); // Edit slugs of checkboxes fields
 
    if ( ! in_array( $form_data['id'], $target_forms ) ) {
        return $field_data;
    }
 
    // split $field_data into separate arrays of $fields and $errors
    list( $fields, $errors ) = $field_data;

    foreach ($target_field_slugs as $key => $target_field_slug) {

        if ( ! isset( $fields[ 'wpcf-' . $target_field_slug ]['value'][0] ) ) {
            $errors[ $target_field_slug ] = 'At least one checkbox is required';
        }
    }
 
    return array( $fields, $errors );
}

I haven't tested that. If it doesn't work as expected, I suggest you familiarise yourself with dumping variables to your error logs which is the easiest way to identify where the code is failing.

For example, send the $field_data variable to your debug.log with

error_log('field_data: ' . print_r($field_data, true));
#1539171

I don't mean to re-open this support ticket, but I just wanted to let you know your code snippet worked perfectly! Thank you