Answers.Body: <blockquote>
    <p>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.</p>
  </blockquote>
  
  <p>If you use <code>'unique' =&gt; true</code> in an attribute on your record, Craft will use Yii's <a href="http://www.yiiframework.com/doc/api/1.1/CUniqueValidator" rel="nofollow noreferrer">CUniqueValidator</a> class to verify the uniqueness of an attribute in the database.</p>
  
  <p><code>$record-&gt;validate()</code> will fail and you can use <code>$record-&gt;getErrors()</code> to get the error message.</p>
  
  <pre><code>return array(
      'key' =&gt; array(AttributeType::Handle, 'default' =&gt; '', 'required' =&gt; true, 'unique' =&gt; true),
  );
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-01-08T15:11:29.660Z
Answers.Text: Ah, got it. `$record->save()` was always returning false, but I wasn't getting the errors from the record validation correctly. Got it working now, thanks!
Answers.UserDisplayName: Trevor Davis
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-01-08T01:06:27.943Z
Answers.LastActivityDate: 2017-01-08T01:06:27.943Z
Answers.UserDisplayName: Brad Bell
Body: <p>I'm building a plugin that has a column in the DB which is <code>unique</code> and essentially an <code>AttributeType::Handle</code>.</p>
  
  <p>Here is the piece from the record:</p>
  
  <pre><code>return array(
      'key' =&gt; array(AttributeType::Handle, 'default' =&gt; '', 'required' =&gt; true),
  );
  </code></pre>
  
  <p>And in my service after the <code>$model</code> has passed validation:</p>
  
  <pre><code>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;
  }
  </code></pre>
  
  <p>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.</p>
  
  <p>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.</p>
  
  <p>Happy to provide more code if necessary, just didn't want to overcomplicate the question. Thanks!</p>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2017-01-07T04:02:29.230Z
Comments.Text: Wow, just realized how dumb I was. I just needed to add the AttributeType::Handle to the model as well. But, I still have the question about throwing an error on uniqueness.
Comments.UserDisplayName: Trevor Davis
Tags: <validation><records><attributetype>
Title: AttributeType::Handle Validation and Unique
ThreadText: # AttributeType::Handle Validation and Unique
  
  (Asked by: username_0 on 2017-01-07)
  
  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' => array(AttributeType::Handle, 'default' => '', 'required' => true),
  );
  
  ```
  
  And in my service after the `$model` has passed validation:
  
  ```
  if ($record->save()) {
      // update id on model (for new records)
      $model->setAttribute('id', $record->getAttribute('id'));
      return true;
  } else {
      $model->addErrors($record->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!
  
  **Tags:** validation, records, attributetype
  
  > **Comments:**
  > **username_0**: Wow, just realized how dumb I was. I just needed to add the AttributeType::Handle to the model as well. But, I still have the question about throwing an error on uniqueness.  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2017-01-08. Score: 0 | Accepted answer
  
  > 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' => true` in an attribute on your record, Craft will use Yii's [CUniqueValidator](http://www.yiiframework.com/doc/api/1.1/CUniqueValidator) class to verify the uniqueness of an attribute in the database.
  
  `$record->validate()` will fail and you can use `$record->getErrors()` to get the error message.
  
  ```
  return array(
      'key' => array(AttributeType::Handle, 'default' => '', 'required' => true, 'unique' => true),
  );
  
  ```
  
  > **Comments:**
  > **username_0**: Ah, got it. `$record->save()` was always returning false, but I wasn't getting the errors from the record validation correctly. Got it working now, thanks!  
  
  ---
  
  