In a Toolset form, you may want to require the user to select at least one checkbox in a custom Checkboxes field.
It is not possible to set up this requirement through the user interface but you can enforce it using the cred_form_validate API hook.
You can use Toolset to add the following code to your site.
Code Snippet
<?php /** * User defined values. * * Fill here the IDs of the Forms that you want to affect with this snippet. * For a single Form, use $target_forms = array( XXX ); * For multiple Forms, use $target_forms = array( XXX, YYY ); * In this example, we target Forms with ID equal to 12345 and 67890. * * Define here the Types checkboxes fields that you want to affect with this snippet. * In this example, we target a Types checkboxes field with a slug of 'hobbies'. */ toolset_snippet_security_check() or die( 'Direct access is not allowed' ); 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 ); $target_field_slug = 'hobbies'; if ( ! in_array( $form_data['id'], $target_forms ) ) { return $field_data; } // $field_data contains fields values and errors list( $fields, $errors ) = $field_data; if ( ! isset( $fields[ 'wpcf-' . $target_field_slug ]['value'][0] ) ) { $errors[ $target_field_slug ] = 'At least one checkbox is required'; } return array( $fields, $errors ); }
Do the following to customize the snippet for your own site:
- Change the 12345,67890 values to a comma-separated list of Toolset forms IDs where you want to apply this validation.
- Change the value of the $target_field_slug variable to match the slug of your custom checkboxes field.