yii: how to make a unique rule for two attributes

后端 未结 8 561
一生所求
一生所求 2020-12-29 02:26

I have a table like this: (id, name, version, text). (name, version) is unique key, how can i make a rule to validate this.

8条回答
  •  灰色年华
    2020-12-29 03:11

    It's very easy. In your array, include a param created in your extensions class.

    Next code is inside Model.

    array('name', 'ext.ValidateNames', 'with'=>'lastname')
    

    Next code is from class ValidateNames into the extensions folder.

    class ValidateNames extends CValidator
    {
        public $with=""; /*my parameter*/
    
        public function validateAttribute($object, $attribute)
        {
            $temp = $this->with;  
            $lastname = $object->$temp;
            $name = $object->$attribute;
            $this->addError($object,$attribute, $usuario." hola ".$lastname);
        }
    }
    

提交回复
热议问题