I have a small table (10 rows) with 2 fields, ID (smallint) and name (varchar 20)
ID is the primary key but not autoincremented
A user can enter/modify the ID and NAME, so I have to check that entered text for ID is numeric and that it’s UNIQE
I set these rules in my model:
return array(
array('id, name'),
array('id','numerical','integerOnly'=>'true'),
array('id','unique'),
array('name', 'length', 'max'=>20),
);
The problem is if the entered text for ID is not a number (eg. "1x")… then the numerical validator sets the error, but the UNIQUE validator IS executed and I get a CDbException !
To solve this I extender the CUniqueValidator like this:
class myUniqueValidator extends CUniqueValidator
{
protected function validateAttribute($object,$attribute)
{
if(!$object->hasErrors($attribute))
parent::validateAttribute($object,$attribute);
}
}
but I think this check should be added to the source of CUniqueValidator so that it’s not executed if there already are some errors for that attribute.