AttributeType::Handle Validation and Unique I'm building a plugin that has a column in the DB which is unique and essentially an AttributeType::Handle.

Here is the piece from the record:

return array(
    'key' =&gt; array(AttributeType::Handle, 'default' =&gt; '', 'required' =&gt; true),
);


And in my service after the $model has passed validation:

if ($record-&gt;save()) {
    // update id on model (for new records)
    $model-&gt;setAttribute('id', $record-&gt;getAttribute('id'));
    return true;
} else {
    $model-&gt;addErrors($record-&gt;getErrors());
    return false;
}


When does that handle validation happen? If I pass something that wouldn't pass handle validation (spaces or a reserved word), it doesn't save the record in the database, but it doesn't throw any errors.

In addition, is there a way to throw an error when the value isn't unique? Again, the record doesn't save if I use a key that is already in the DB, but there is no error.

Happy to provide more code if necessary, just didn't want to overcomplicate the question. Thanks!
In addition, is there a way to throw an error when the value isn't unique? Again, the record doesn't save if I use a key that is already in the DB, but there is no error.


If you use 'unique' =&gt; true in an attribute on your record, Craft will use Yii's CUniqueValidator class to verify the uniqueness of an attribute in the database.

$record-&gt;validate() will fail and you can use $record-&gt;getErrors() to get the error message.

return array(
    'key' =&gt; array(AttributeType::Handle, 'default' =&gt; '', 'required' =&gt; true, 'unique' =&gt; true),
);