Answers.Body: <p>If your model somehow extends Yii's <a href="https://www.yiiframework.com/doc/api/2.0/yii-base-model" rel="nofollow noreferrer">Model</a> (which I think it does because you use Yii validators), you can use the <a href="https://www.yiiframework.com/doc/api/2.0/yii-base-model#beforeValidate()-detail" rel="nofollow noreferrer"><code>beforeValidate()</code></a> method to change attribute values before validation:</p>
  
  <pre><code>public function beforeValidate()
  {
      // Before validation, normalize the attribute's value with whatever method you create for it
      $this-&gt;attribute = $this-&gt;yourNormalizationMethod($this-&gt;attribute);
  }
  </code></pre>
  
  <p>After that, you can use Yii's <a href="https://www.yiiframework.com/doc/api/2.0/yii-validators-uniquevalidator" rel="nofollow noreferrer">UniqueValidator</a> to make sure the attribute's value is unique. The docs show you some of it's possible configs, I'm sure you can make it work!</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-05-06T15:14:01.943Z
Answers.Text: Thanks for your anwser.
  Unfortunately, the "beforeValidate()" method does not seems to fire up.
  public function beforeValidate() {
   return false;
  }
  does not raise anything in the Craft CP.
  Where should I declare this function?
  Do you have any example of this method working in a Field class?
Answers.UserDisplayName: scandella
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-03-01T14:31:22.773Z
Answers.LastActivityDate: 2020-03-01T14:31:22.773Z
Answers.UserDisplayName: Paul
Body: <p>I have a custom text field in a module that the user populate with an EAN number. The input is getting validate by a "match" validator and then is serialized before save to get ride of the various variants of the EAN the user can enter ("978-2-87929-647-0", "978.2.87929.647.0" and so on…).
  But now I need to validate is uniqueness and I can't find the right way to do this.</p>
  
  <p>How can I normalize the input and then test if this normalized value already exists in the DB?</p>
  
  <p>I cannot find a way to get the actual user input and normalize it before validate it.</p>
  
  <p>I guess I have to use the "UniqueValidator" to test if the entry already exists in the DB.</p>
  
  <p>Thanks for your help. </p>
  
Tags: <modules><custom-field><validation>
Title: Normalize then validate unique entry in a custom module field
ThreadText: # Normalize then validate unique entry in a custom module field
  
  (Asked by: username_0 on 2020-03-01)
  
  I have a custom text field in a module that the user populate with an EAN number. The input is getting validate by a "match" validator and then is serialized before save to get ride of the various variants of the EAN the user can enter ("978-2-87929-647-0", "978.2.87929.647.0" and so on…).
  But now I need to validate is uniqueness and I can't find the right way to do this.
  
  How can I normalize the input and then test if this normalized value already exists in the DB?
  
  I cannot find a way to get the actual user input and normalize it before validate it.
  
  I guess I have to use the "UniqueValidator" to test if the entry already exists in the DB.
  
  Thanks for your help.
  
  **Tags:** modules, custom-field, validation
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2020-03-01. Score: 1
  
  If your model somehow extends Yii's [Model](https://www.yiiframework.com/doc/api/2.0/yii-base-model) (which I think it does because you use Yii validators), you can use the [`beforeValidate()`](https://www.yiiframework.com/doc/api/2.0/yii-base-model#beforeValidate()-detail) method to change attribute values before validation:
  
  ```
  public function beforeValidate()
  {
      // Before validation, normalize the attribute's value with whatever method you create for it
      $this->attribute = $this->yourNormalizationMethod($this->attribute);
  }
  
  ```
  
  After that, you can use Yii's [UniqueValidator](https://www.yiiframework.com/doc/api/2.0/yii-validators-uniquevalidator) to make sure the attribute's value is unique. The docs show you some of it's possible configs, I'm sure you can make it work!
  
  > **Comments:**
  > **username_0**: Thanks for your anwser. Unfortunately, the "beforeValidate()" method does not seems to fire up. public function beforeValidate() {  return false; } does not raise anything in the Craft CP. Where should I declare this function? Do you have any example of this method working in a Field class?  
  
  ---
  
  