Answers.Body: <p>First of all: this validator requires an active record as target in order to know what table it should check to search for uniqueness. Since Craft has not such a thing you would need to create a record for the <code>content</code> table in order to make it work. </p>
  
  <p>Furthermore you'll need to check the context of the field, if it's inside a matrix or inside a super table field or whatever so you need to set the target Attribute dynamically based on the table.</p>
  
  <p>Finally it won't serialize your value automatically and search for the string, instead it will just fail because of <a href="https://github.com/yiisoft/yii2/blob/master/framework/validators/UniqueValidator.php#L133" rel="nofollow noreferrer">these</a> lines </p>
  
  <pre><code>foreach ($rawConditions as $key =&gt; $value) { 
      if (is_array($value)) { 
          $this-&gt;addError($model, $attribute, Yii::t('yii', '{attribute} is invalid.')); 
           return; 
      } 
      $conditions[] = [$key =&gt; $value];
  }
  </code></pre>
  
  <p>Your value is an array so it will add an error no matter what </p>
  
  <p>However: you can rebuild the functionality by actually validating the model by yourself with this validator instead of defining a validator and letting Craft handling it. </p>
  
  <p>You can see an example in the <a href="https://github.com/craftcms/cms/blob/develop/src/fields/Matrix.php#L515" rel="nofollow noreferrer">matrix field</a> it inserts just a function that is being called in the class instead of a validator. Then you can create a <a href="https://www.yiiframework.com/doc/api/2.0/yii-base-dynamicmodel" rel="nofollow noreferrer">dynamic model </a> and validate that one. You can read more about  <a href="https://yii2-framework.readthedocs.io/en/latest/guide/input-validation/" rel="nofollow noreferrer">ad hock validations</a></p>
  
  <pre><code>$email = 'test@example.com';
  $validator = new yii\validators\EmailValidator(); 
  if ($validator-&gt;validate($email, $error)) { 
      echo 'Email is valid.'; 
  } else { 
      echo $error; 
  }
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-10-07T22:05:47.523Z
Answers.Text: Very informative comment, thank you very much. Could you suggest some resources on Records (Active Records since Craft 3?)? There isn't much in terms of documentation that I can find.
Answers.UserDisplayName: Dwarf Vader
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-10-10T17:53:17.980Z
Answers.Text: @DwarfVader Probably best to go to the Yii 2 docs for that: https://www.yiiframework.com/doc/guide/2.0/en/db-active-record
Answers.UserDisplayName: Brad Bell
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-10-07T13:22:46.530Z
Answers.LastActivityDate: 2018-10-07T14:03:26.913Z
Answers.LastEditDate: 2018-10-07T14:03:26.913Z
Answers.UserDisplayName: Robin Schambach
Body: <p>I am learning to write my first plugin for Craft CMS 3.x</p>
  
  <p>I have a field that gets displayed as a string, but is stored in the database as binary. When I validate it, I write:</p>
  
  <pre><code>public function getElementValidationRules(): array
  {
      return [
          /* Other rules, */
          ['unique']
      ];
  }
  </code></pre>
  
  <p>What I wonder is, will it get compared in its string form to the binary? Or will serializeValue() get called?</p>
  
Tags: <plugin-development><validation><serialization>
Title: Do field values get serialized before validation?
ThreadText: # Do field values get serialized before validation?
  
  (Asked by: username_0 on 2018-10-07)
  
  I am learning to write my first plugin for Craft CMS 3.x
  
  I have a field that gets displayed as a string, but is stored in the database as binary. When I validate it, I write:
  
  ```
  public function getElementValidationRules(): array
  {
      return [
          /* Other rules, */
          ['unique']
      ];
  }
  
  ```
  
  What I wonder is, will it get compared in its string form to the binary? Or will serializeValue() get called?
  
  **Tags:** plugin-development, validation, serialization
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2018-10-07. Score: 2 | Accepted answer
  
  First of all: this validator requires an active record as target in order to know what table it should check to search for uniqueness. Since Craft has not such a thing you would need to create a record for the `content` table in order to make it work.
  
  Furthermore you'll need to check the context of the field, if it's inside a matrix or inside a super table field or whatever so you need to set the target Attribute dynamically based on the table.
  
  Finally it won't serialize your value automatically and search for the string, instead it will just fail because of [these](https://github.com/yiisoft/yii2/blob/master/framework/validators/UniqueValidator.php#L133) lines
  
  ```
  foreach ($rawConditions as $key => $value) { 
      if (is_array($value)) { 
          $this->addError($model, $attribute, Yii::t('yii', '{attribute} is invalid.')); 
           return; 
      } 
      $conditions[] = [$key => $value];
  }
  
  ```
  
  Your value is an array so it will add an error no matter what
  
  However: you can rebuild the functionality by actually validating the model by yourself with this validator instead of defining a validator and letting Craft handling it.
  
  You can see an example in the [matrix field](https://github.com/craftcms/cms/blob/develop/src/fields/Matrix.php#L515) it inserts just a function that is being called in the class instead of a validator. Then you can create a [dynamic model](https://www.yiiframework.com/doc/api/2.0/yii-base-dynamicmodel)  and validate that one. You can read more about [ad hock validations](https://yii2-framework.readthedocs.io/en/latest/guide/input-validation/)
  
  ```
  $email = 'test@example.com';
  $validator = new yii\validators\EmailValidator(); 
  if ($validator->validate($email, $error)) { 
      echo 'Email is valid.'; 
  } else { 
      echo $error; 
  }
  
  ```
  
  > **Comments:**
  > **username_0**: Very informative comment, thank you very much. Could you suggest some resources on Records (Active Records since Craft 3?)? There isn't much in terms of documentation that I can find.  
  > **username_2**: @username_0 Probably best to go to the Yii 2 docs for that: https://www.yiiframework.com/doc/guide/2.0/en/db-active-record  
  
  ---
  
  