<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>

CC BY-SA 4.0
2018-10-07T22:05:47.523Z
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.
Dwarf Vader
CC BY-SA 4.0
2018-10-10T17:53:17.980Z
@DwarfVader Probably best to go to the Yii 2 docs for that: https://www.yiiframework.com/doc/guide/2.0/en/db-active-record
Brad Bell
CC BY-SA 4.0
2018-10-07T13:22:46.530Z
2018-10-07T14:03:26.913Z
2018-10-07T14:03:26.913Z
Robin Schambach
<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>

<plugin-development><validation><serialization>
Do field values get serialized before validation?
# 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 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 it inserts just a function that is being called in the class instead of a validator. Then you can create a dynamic model  and validate that one. You can read more about  ad hock validations
$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**: @DwarfVader Probably best to go to the Yii 2 docs for that: https://www.yiiframework.com/doc/guide/2.0/en/db-active-record  

---

