Forms Yii2需求选择FormBuilder中的一个或多个复选框

Forms Yii2需求选择FormBuilder中的一个或多个复选框,forms,validation,checkbox,yii2,Forms,Validation,Checkbox,Yii2,我在“Yii2 krajee FormBuilder”中创建了一份登记表。它包含两个复选框。必须至少选择其中一个。必须在客户端执行验证,以避免页面过载。如果Yii2包含将所需规则和whenClient分配给复选框的选项,那么一切都会很容易——不幸的是,这是不可能的。对于其他字段(文本框、选择框),您可以使用以下代码: 型号: $public $module1; $public $module2; 'module1'=>[ 'type'=>Form

我在“Yii2 krajee FormBuilder”中创建了一份登记表。它包含两个复选框。必须至少选择其中一个。必须在客户端执行验证,以避免页面过载。如果Yii2包含将所需规则和whenClient分配给复选框的选项,那么一切都会很容易——不幸的是,这是不可能的。对于其他字段(文本框、选择框),您可以使用以下代码:

型号:

$public $module1;
$public $module2;

  'module1'=>[
                'type'=>Form::INPUT_CHECKBOX,
                'name'=>'ak1',
                'label'=>'<b>'.Yii::t('app','register.module1').'</b>' ],

  'module2'=>[
                'type'=>Form::INPUT_CHECKBOX,
                'name'=>'ak2',
                'label'=>'<b>'.Yii::t('app','register.module2').'</b>' ]

'textbox1'=>[
                    'type'=>Form::INPUT_TEXTBOX,
                    'name'=>'tx1',
                    'label'=>'<b>'.Yii::t('app','register.tx1').'</b>' ]
[...]


     [[  'textbox1', 'texbox2', ],'required','whenClient'=> "function (attribute, value) { return  $('#registerform-textbox1').is(':checked') == false && $('#registerform-textbox2').is(':checked') == false;}" 
],
通常,复选框验证由以下人员执行:

public function rules () 
     {
         array ['checboxname', 'compare', 'compareValue' => true, 
               'message' => 'You must agree to the terms and conditions'], 
         ... 
     } 
但是在这种情况下,您不能将rulecompare与rulewhenclient结合起来,后者负责在客户端进行函数指定的验证。
我怎样才能解决这个问题呢?

我不太确定您在尝试什么,但我认为您会:

['checkbox1', 'required', 'when' => function($model) {
    return $model->checkbox2 == false;  
  }
],
['checkbox2', 'required', 'when' => function($model) {
    return $model->checkbox1 == false;  
  }
],
['checkbox1', 'required', 'when' => function($model) {
    return $model->checkbox2 == false;  
  }
],
['checkbox2', 'required', 'when' => function($model) {
    return $model->checkbox1 == false;  
  }
],