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

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

PHP中文网
PHP中文网Original
2016-05-25 17:03:281390browse

1.在AR的rules中的定义

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

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);
    }
}
Statement:
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