説明

wpforms_process_validate_checkboxアクションは、フォーム送信時にチェックボックスフォームフィールドの検証を実行します。

パラメーター

$field_id
(int) フィールドID。
$field_submit
(array) フィールドに送信された元の未加工/未サニタイズのフィールド値。
$form_data
(配列)  後で使用するために処理および準備されたフォーム設定/データ。

ソース

wpforms/includes/class-process.php

詳細情報

wpforms_process_validate_checkboxフィルターは、チェックボックスフォームフィールドの配列に適用されます。この関数は、すべてのフォームフィールドdo_action( "wpforms_process_validate_{$field_type}", $field_id, $field_submit, $form_data )に使用できます。

フィールド値は、後で wpforms_process_format_{$field_type} でサニタイズされることに注意することが重要です。the

以下の例では、関数は選択する選択肢の最小数についてチェックボックスフォームフィールドをチェックします。

/*
 * 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 );



記事参照:チェックボックスの選択肢の最小数を設定する方法