条件验证,如果复选框被选中,则其他字段是必需的
Conditional validation, if a checkbox is checked other fields are required
我需要通过以下方式验证表单:
我有一个复选框和 3 个文本字段,如果前者被选中,则后者是必需的。
有没有可能实现这个"out of the box"?
是的,这是可能的。我建议阅读有关输入验证的 Yii 文档,尤其是有关条件验证的部分:http://www.yiiframework.com/doc-2.0/guide-input-validation.html#conditional-validation
我猜你最终会使用类似(未测试)的东西:
[
['my_attribute', 'required', 'when' => function($model) {
return $model->my_checkbox === true;
}],
]
我需要通过以下方式验证表单:
我有一个复选框和 3 个文本字段,如果前者被选中,则后者是必需的。
有没有可能实现这个"out of the box"?
是的,这是可能的。我建议阅读有关输入验证的 Yii 文档,尤其是有关条件验证的部分:http://www.yiiframework.com/doc-2.0/guide-input-validation.html#conditional-validation
我猜你最终会使用类似(未测试)的东西:
[
['my_attribute', 'required', 'when' => function($model) {
return $model->my_checkbox === true;
}],
]