Home > php教程 > PHP源码 > Yii中对CUniqueValidator使用附加字段结合判断

Yii中对CUniqueValidator使用附加字段结合判断

PHP中文网
Release: 2016-05-25 17:03:28
Original
1384 people have browsed it

1.在AR的rules中的定义

public function rules()
{
    return array(
        array('name', 'unique', 'criteria' => array('condition' => "`user_site_id`=>'{$this->user_site_id}'")),
    );
}
Copy after login

2.自定义验证规则

public function rules()
{
    return array(
        array('name', 'validateSiteUnique');
    );
}
 
public function validateSiteUnique($attribute, $param)
{
    if (!$this->hasErrors()) {
        $siteUniqueCriteria = new CDbCriteria();
        $siteUniqueCriteria->addCondition("`user_site_id`='{$this->user_site_id}'");
        $validator = new CUniqueValidator();
        $validator->attributes = array($attribute);
        $validator->criteria = $siteUniqueCriteria;
        $validator->validate($this);
    }
}
Copy after login
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template