yii2-验证规则,rules,判断条件

yii2模型的验证规则,简单的使用我就不详细说了,想看的可以去看
官网教程
http://www.yiichina.com/doc/guide/2.0/structure-models#validation-rules
社区网友教程
http://www.yiichina.com/topic/6420
这里我想说一下,rules规则里面,加判断条件的情况
举个例子,比如我想实现两个字段至少有一个是必填的条件,这时就需要在required里面加判断条件了,
之前查资料国内文档都没有写如何做,laravel框架有required_without来判断
后来在stackoverflow上找到了可以加when和whenclient条件,上面网友教程也提到了,这是我在Google上找到相关信息后,
又来百度查,只找到这一篇,但是相关内容写的不详细。
when 代表服务器端的判断条件,生效条件。
whenclient 代表是客户端的判断条件,js生效条件。
以下是我的代码,仅供参考

            [
                'executor_id',
                'required',
                'when' => function ($model) {
                    return empty($model->doctor_id);
                },
                'whenClient' => "function (attribute, value) {
                    return $('#doctor_id').value == '';
                }",
                 'on' => 'assgin',
            ],
            [
                'doctor_id',
                'required',
                'when' => function ($model) {
                    return empty($model->executor_id);
                },
                'whenClient' => "function (attribute, value) {
                    return $('#executor_id').value == '';
                }",
                'on' => 'assgin',
            ]        


总结:掌握高超了搜索技术对于查找所需知识是非常重要的啊

原文地址:https://www.cnblogs.com/hikoming/p/6677715.html