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)
$this->addError($attribute,....)
}
protected function dbCheckUnique($surname,$name,$code)
{
return ....//check db
}