Description

The wpforms_process_validate_checkbox action fires validation on the Checkbox form field when the form is submitted.

Parameters

$field_id
(int) Field ID.
$field_submit
(array) Original raw/unsanitized field value submitted for the field.
$form_data
(array) Processed form settings/data, prepared to be used later.

Source

wpforms/includes/class-process.php

More Information

The wpforms_process_validate_checkbox filter is applied to an array for Checkbox form field. This function can be used for all form fields do_action( "wpforms_process_validate_{$field_type}", $field_id, $field_submit, $form_data ).

It’s important to note that field values are not sanitized until later on in the processing, at wpforms_process_format_{$field_type}.the

Examples

In this example shown below, the function will check the Checkboxes form field for a minimum number of choices to be selected.

/*
 * Check the Checkboxes field for minimum number of choices.
 *
 * @link https://wpforms.com/developers/wpforms_process_validate_checkbox/
 *
 * @param int     $field_id        Field ID.
 * @param array   $field_submit    Unsanitized field value submitted for the field.
 * @param array   $form_data       Form data and settings.
*/

function wpf_checkbox_validation( $field_id, $field_submit, $form_data ) {
 
    $field_submit  = (array) $field_submit;
 
    $count_choices = count( $field_submit );
    if ( $count_choices < 2 ) {
        wpforms()->process->errors[ $form_data[ 'id' ] ][ $field_id ] = __( 'Please select at least 2 options', 'plugin-domain' );
 
    }
 
}
add_action( 'wpforms_process_validate_checkbox', 'wpf_checkbox_validation', 10, 3 );



Article Reference: How to Set a Minimum Number of Choices for a Checkbox