custom validation with a couple of attributes

I need to write custom validation for models. If validation fails, I need to add vadiation error to standart validation summary.

I understand how to create custom validation rule, if I have to validate one attribute for some values.

However, I need to take three attributes (surname+name+code) from user input and validate if their combination doesn’t exist in database (I will use Model Exists or Model FindByAttribytes). If exists - than validation fails.



In the implementation of the rules method you have access to all current attributes/values by using ‘$this’

public function rules()


      return array(

	 array('surname,name,code', 'checkUnique')



public function checkUnique($attribute)


  if (!$this->dbCheckUnique($this->surname,$this->name,$this->code)



protected function dbCheckUnique($surname,$name,$code) 


   return ....//check db


It will run the validation method 3 times. This rule will be enough:

array('surname', 'checkUnique')

in general, to validate several attributes you can access all of them in your validator through $this

as for your example with uniqueness, take a look at my extension