Answers.Body: <p>Have a look at Stephen Lewis' <a href="https://github.com/manifestuk/2016-10-blog-archive/blob/master/posts/2014-07-29--the-definitive-guide-to-validation-in-craft-part-1-foundations.yml" rel="nofollow noreferrer">archived blog article</a> about validation. Even better, <a href="https://leanpub.com/craft-validation" rel="nofollow noreferrer">buy the book</a>.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-10-29T23:58:49.013Z
Answers.Text: Also [this thread](http://craftcms.stackexchange.com/questions/9362/defineattributes-and-rules)
Answers.UserDisplayName: Damon
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2015-10-29T22:47:34.013Z
Answers.LastActivityDate: 2021-01-14T13:40:55.273Z
Answers.LastEditDate: 2021-01-14T13:40:55.273Z
Answers.UserDisplayName: carlcs
Body: <p>I'm trying to get a better understanding of how to define new models in plugins, but the documentation at <a href="http://buildwithcraft.com/docs/plugins/models" rel="nofollow">http://buildwithcraft.com/docs/plugins/models</a> doesn't get me very far.</p>
  
  <p>For example, there is:</p>
  
  <pre><code>protected function defineAttributes()
  {
      return array(
          'name' =&gt; AttributeType::String,
          'type' =&gt; array(AttributeType::Enum, 'values' =&gt; "alcohol,mixer,other"),
      );
  }`
  </code></pre>
  
  <p>I've seen other plugins define attributes with an array element of <code>'required' =&gt; true</code>, but the only example I see here is <code>'values'</code>. Is there somewhere I can see a list of the available options when defining attributes? Or is it a better practice to define the attributes simply and then define validation rules in the <code>rules()</code> method?</p>
  
  <p>I know the docs say to check out the CModel documentation, but it doesn't even have defineAttributes. Does Craft itself have more in depth documentation on this topic?</p>
  
Tags: <plugin-development><models>
Title: More extensive documentation for defining models for plugins
ThreadText: # More extensive documentation for defining models for plugins
  
  (Asked by: username_0 on 2015-10-29)
  
  I'm trying to get a better understanding of how to define new models in plugins, but the documentation at <http://buildwithcraft.com/docs/plugins/models> doesn't get me very far.
  
  For example, there is:
  
  ```
  protected function defineAttributes()
  {
      return array(
          'name' => AttributeType::String,
          'type' => array(AttributeType::Enum, 'values' => "alcohol,mixer,other"),
      );
  }`
  
  ```
  
  I've seen other plugins define attributes with an array element of `'required' => true`, but the only example I see here is `'values'`. Is there somewhere I can see a list of the available options when defining attributes? Or is it a better practice to define the attributes simply and then define validation rules in the `rules()` method?
  
  I know the docs say to check out the CModel documentation, but it doesn't even have defineAttributes. Does Craft itself have more in depth documentation on this topic?
  
  **Tags:** plugin-development, models
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2015-10-29. Score: 1
  
  Have a look at Stephen Lewis' [archived blog article](https://github.com/manifestuk/2016-10-blog-archive/blob/master/posts/2014-07-29--the-definitive-guide-to-validation-in-craft-part-1-foundations.yml) about validation. Even better, [buy the book](https://leanpub.com/craft-validation).
  
  > **Comments:**
  > **username_2**: Also [this thread](http://craftcms.stackexchange.com/questions/9362/defineattributes-and-rules)  
  
  ---
  
  