我在"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;}"
],
它适用于文本框的..but。不能将复选框分配给所需
我使用了这个,但是在这个例子中,页面被重新加载。
['module1',function ($attribute, $params) {
if ($this->module1 == 0 && $this->module2 == 0) {
$this->addError($attribute, 'you have to select at least one option');
}
}],
通常,复选框验证由
public function rules ()
{
array ['checboxname', 'compare', 'compareValue' => true,
'message' => 'You must agree to the terms and conditions'],
...
}
但在这种情况下,您不能将规则与规则whenClient (负责函数指定的客户端验证)进行组合。我该如何解决这个问题?
发布于 2017-10-26 06:04:20
我不太清楚你在尝试什么,但我认为你会这样做:
['checkbox1', 'required', 'when' => function($model) {
return $model->checkbox2 == false;
}
],
['checkbox2', 'required', 'when' => function($model) {
return $model->checkbox1 == false;
}
],
https://stackoverflow.com/questions/26561587
复制相似问题