Skip to content

Commit

Permalink
Apply maxitems=1 validation
Browse files Browse the repository at this point in the history
Fixes #1445
  • Loading branch information
takayukister committed Jun 28, 2024
1 parent 5ba998b commit 89de571
Showing 1 changed file with 21 additions and 7 deletions.
28 changes: 21 additions & 7 deletions modules/checkbox.php
Original file line number Diff line number Diff line change
Expand Up @@ -196,16 +196,30 @@ function wpcf7_checkbox_form_tag_handler( $tag ) {

function wpcf7_swv_add_checkbox_rules( $schema, $contact_form ) {
$tags = $contact_form->scan_form_tags( array(
'type' => array( 'checkbox*', 'radio' ),
'basetype' => array( 'checkbox', 'radio' ),
) );

foreach ( $tags as $tag ) {
$schema->add_rule(
wpcf7_swv_create_rule( 'required', array(
'field' => $tag->name,
'error' => wpcf7_get_message( 'invalid_required' ),
) )
);
if ( $tag->is_required() or 'radio' === $tag->type ) {
$schema->add_rule(
wpcf7_swv_create_rule( 'required', array(
'field' => $tag->name,
'error' => wpcf7_get_message( 'invalid_required' ),
) )
);
}

if ( 'radio' === $tag->type or $tag->has_option( 'exclusive' ) ) {
$schema->add_rule(
wpcf7_swv_create_rule( 'maxitems', array(
'field' => $tag->name,
'threshold' => 1,
'error' => $contact_form->filter_message(
__( "Too many items are selected.", 'contact-form-7' )
),
) )
);
}
}
}

Expand Down

0 comments on commit 89de571

Please sign in to comment.