Answers.Body: <p>Your best bet is going to be to write your own <a href="http://buildwithcraft.com/docs/plugins/field-types" rel="nofollow">field type</a> for this. You can extend one of the built-in field types if you want.</p>
  
  <p>You would put your custom validation logic in your field type’s <a href="http://buildwithcraft.com/classreference/fieldtypes/IFieldType#validate-detail" rel="nofollow">validate()</a> method.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-11-11T19:42:52.467Z
Answers.Text: Thanks, Brandon. We're using text, textarea and select fields, so we'd have to create 3 custom field types just to add some basic validation in. Is there no simpler way other than validating directly in the service?
Answers.UserDisplayName: Russ Back
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-11-26T12:10:09.917Z
Answers.Text: I ended up creating a simple text field that validates uniqueness across the relevant ElementType: https://github.com/fuel-digital/Craft-Unique-Text-FieldType
Answers.UserDisplayName: Russ Back
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-11-11T18:56:20.073Z
Answers.LastActivityDate: 2014-11-11T18:56:20.073Z
Answers.UserDisplayName: Brandon Kelly
Body: <p>I've been looking at <a href="http://straightupcraft.com/articles/the-definitive-guide-to-validation-in-craft" rel="nofollow">http://straightupcraft.com/articles/the-definitive-guide-to-validation-in-craft</a> which seems to concentrate on custom models. However I am looking to validate data posted to a plugin that stores that data as core EntryModels.</p>
  
  <p>So let's say you create a Section and assign some Fields in its field layout. You can set each Field to be required but I want to do more than that and check if it's an integer or if it's unique.</p>
  
  <p>I am currently doing that in my service class:</p>
  
  <pre><code>private function validateReference()
  {
      $reference = craft()-&gt;request-&gt;getPost('reference');
  
      $existingEntryCount = craft()-&gt;elements-&gt;getCriteria(ElementType::Entry)-&gt;total(array(
          'reference' =&gt; $reference
      ));
  
      return ($existingEntryCount === 0);
  }
  </code></pre>
  
  <p>That all works but I am wondering if instead I should be setting the validation properties of each Field in this service and then just running a core validate method?</p>
  
Tags: <plugin-development><validation>
Title: Adding custom validation to core fields
ThreadText: # Adding custom validation to core fields
  
  (Asked by: username_0 on 2014-11-11)
  
  I've been looking at <http://straightupcraft.com/articles/the-definitive-guide-to-validation-in-craft> which seems to concentrate on custom models. However I am looking to validate data posted to a plugin that stores that data as core EntryModels.
  
  So let's say you create a Section and assign some Fields in its field layout. You can set each Field to be required but I want to do more than that and check if it's an integer or if it's unique.
  
  I am currently doing that in my service class:
  
  ```
  private function validateReference()
  {
      $reference = craft()->request->getPost('reference');
  
      $existingEntryCount = craft()->elements->getCriteria(ElementType::Entry)->total(array(
          'reference' => $reference
      ));
  
      return ($existingEntryCount === 0);
  }
  
  ```
  
  That all works but I am wondering if instead I should be setting the validation properties of each Field in this service and then just running a core validate method?
  
  **Tags:** plugin-development, validation
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2014-11-11. Score: 1 | Accepted answer
  
  Your best bet is going to be to write your own [field type](http://buildwithcraft.com/docs/plugins/field-types) for this. You can extend one of the built-in field types if you want.
  
  You would put your custom validation logic in your field type’s [validate()](http://buildwithcraft.com/classreference/fieldtypes/IFieldType#validate-detail) method.
  
  > **Comments:**
  > **username_0**: Thanks, username_1. We're using text, textarea and select fields, so we'd have to create 3 custom field types just to add some basic validation in. Is there no simpler way other than validating directly in the service?  
  > **username_0**: I ended up creating a simple text field that validates uniqueness across the relevant ElementType: https://github.com/fuel-digital/Craft-Unique-Text-FieldType  
  
  ---
  
  