简体   繁体   English

如何在rules()方法中获得验证?

[英]How to get the validation in rules() method?

Just for explanation,here is the table 只是为了解释,这是表格

create table test(mark int,item1 varchar(128),item2 varchar(128));

If mark==1 then item1 requires a value ,if mark==2 then item2 requires a value 如果mark == 1,则item1需要一个值;如果mark === 2,则item2需要一个值

In rules() method,how can I get this rule? 在rules()方法中,如何获得此规则?

Create your custom validating function in the model, smth. 在模型smth中创建自定义验证函数。 like this: 像这样:

public function requiredMark($attributes,$params)
{
    if ($this->mark == 1 && $this->item1==null)
        $this->addError('item1','Item 1 is required');
    elseif ($this->mark == 2 && $this->item2==null)
        $this->addError('item2','Item 2 is required');
}

Or u was asking about something other? 还是您在问其他问题?

You'll need to extend the validate() method to do this, and add errors manually. 您需要扩展validate()方法来执行此操作,并手动添加错误。 I always get back to this document for validator options (it might be useful later on) 我总是返回本文档以获取验证器选项(以后可能会有用)

http://yii.googlecode.com/files/yii-1.1.0-validator-cheatsheet.pdf http://yii.googlecode.com/files/yii-1.1.0-validator-cheatsheet.pdf

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM