Answers.Body: <p>It turns out that because I'm now using my own settings model, I don't need to use <code>AttributeType::Mixed</code> for my date fields any more. (Thanks to carlcs for suggesting this.)</p>
  
  <p>So I've removed the stuff <a href="https://craftcms.stackexchange.com/questions/8348/how-do-i-save-a-plugins-date-settings-field/8358#8358">I had in my Plugin's <code>getSettingsHtml()</code></a>, which massaged the fields before sending them to the template. And now, in theory, I only need to do this:</p>
  
  <pre class="lang-php prettyprint-override"><code>&lt;?php
  namespace Craft;
  
  class My_SettingsModel extends BaseModel
  {
      protected function defineAttributes()
      {
          return array(
              'myText' =&gt; array(AttributeType::String,
                              'column' =&gt; ColumnType::Text, 'required' =&gt; true),
              'myDate' =&gt; array(AttributeType::DateTime,
                              'column' =&gt; ColumnType::DateTime),
          );
      }
  
      public function rules()
      {
          $rules = parent::rules();
  
          $rules[] = array('myDate', 'date',
                          'format' =&gt; array('dd/MM/yyyy'),
                          'allowEmpty' =&gt; false,
                          'message' =&gt; 'Please enter a valid date');
  
          return $rules;
      }
   }
  </code></pre>
  
  <p>However, although the form requires me to enter dates - it doesn't accept empty fields - it won't accept the dates I'm typing in. e.g., if I enter <code>31/12/2016</code> it fails the date validation. Looking at Yii's <a href="http://www.yiiframework.com/doc/api/1.1/CDateTimeParser" rel="nofollow noreferrer">date validation docs</a> I think this should work...</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-01-18T12:47:46.117Z
Answers.LastActivityDate: 2016-01-18T12:57:12.160Z
Answers.LastEditDate: 2017-04-13T12:51:00.253Z
Answers.UserDisplayName: Phil Gyford
Body: <p>I have a plugin that has some settings, some of which are dates. I want to make these fields required, and ensure they are valid dates.</p>
  
  <p>Because of <a href="https://craftcms.stackexchange.com/questions/8348/how-do-i-save-a-plugins-date-settings-field">this bug</a> the date attributes are set as <code>AttributeType::Mixed</code>.</p>
  
  <p>I'm using a structure like <a href="https://craftcms.stackexchange.com/questions/10746/custom-validation-for-plugin-settings">this</a> and <a href="https://craftcms.stackexchange.com/questions/10162/how-can-i-add-custom-validation-rules-to-my-plugin-settings">this</a> to set up the attributes on my settings, but I can't get the date fields to be required, never mind use a custom date-validation method.</p>
  
  <pre class="lang-php prettyprint-override"><code>&lt;?php
  namespace Craft;
  
  class My_SettingsModel extends BaseModel
  {
      protected function defineAttributes()
      {
          return array(
              'myText' =&gt; array(AttributeType::String, 'column' =&gt; ColumnType::Text),
              // There are multiple date fields, but for the example:
              'myDate' =&gt; array(AttributeType::Mixed),
          );
      }
  
      public function rules()
      {
          $rules = parent::rules();
  
          $rules[] = array('myText', 'required');
          $rules[] = array('myDate', 'required');
          $rules[] = array('myDate', 'validDateString');
  
          return $rules;
      }
  
  
       public function validDateString($attribute, $params)
       {
           if ( /*  $this-&gt;$attribute is not a valid date  */ )
           {
               $this-&gt;addError($attribute, 'Please enter a valid date');
           }
       }
   }
  </code></pre>
  
  <p><code>myText</code> is required OK - the form re-displays with an error message if you try to leave it empty.</p>
  
  <p>I have two problems:</p>
  
  <ol>
  <li><code>myDate</code> is not required - if I leave the date field empty, the form is always successfully submitted, and when I go back to it the field has the date that was previously there.</li>
  <li>Ignoring that, although <code>validDateString()</code> gets called, and doing <code>var_dump($this-&gt;$attribute)</code> looks something like <code>array(1) { ["date"]=&gt; string(0) "18/01/2016" }</code>, I get "Illegal string offset 'date'" if I try to access <code>$this-&gt;$attribute['date']</code>. I'm baffled.</li>
  </ol>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2016-01-18T11:05:11.400Z
Comments.Text: Incidentally, the data coming back as an array with a `"date"` key because I'm having to manage the date fields [like this](http://craftcms.stackexchange.com/questions/8348/how-do-i-save-a-plugins-date-settings-field/8358#8358).
Comments.UserDisplayName: Phil Gyford
Tags: <plugin-development><validation>
Title: Validating date fields in a plugin's settings
ThreadText: # Validating date fields in a plugin's settings
  
  (Asked by: username_0 on 2016-01-18)
  
  I have a plugin that has some settings, some of which are dates. I want to make these fields required, and ensure they are valid dates.
  
  Because of [this bug](https://craftcms.stackexchange.com/questions/8348/how-do-i-save-a-plugins-date-settings-field) the date attributes are set as `AttributeType::Mixed`.
  
  I'm using a structure like [this](https://craftcms.stackexchange.com/questions/10746/custom-validation-for-plugin-settings) and [this](https://craftcms.stackexchange.com/questions/10162/how-can-i-add-custom-validation-rules-to-my-plugin-settings) to set up the attributes on my settings, but I can't get the date fields to be required, never mind use a custom date-validation method.
  
  ```php
  <?php
  namespace Craft;
  
  class My_SettingsModel extends BaseModel
  {
      protected function defineAttributes()
      {
          return array(
              'myText' => array(AttributeType::String, 'column' => ColumnType::Text),
              // There are multiple date fields, but for the example:
              'myDate' => array(AttributeType::Mixed),
          );
      }
  
      public function rules()
      {
          $rules = parent::rules();
  
          $rules[] = array('myText', 'required');
          $rules[] = array('myDate', 'required');
          $rules[] = array('myDate', 'validDateString');
  
          return $rules;
      }
  
       public function validDateString($attribute, $params)
       {
           if ( /*  $this->$attribute is not a valid date  */ )
           {
               $this->addError($attribute, 'Please enter a valid date');
           }
       }
   }
  
  ```
  
  `myText` is required OK - the form re-displays with an error message if you try to leave it empty.
  
  I have two problems:
  
  1. `myDate` is not required - if I leave the date field empty, the form is always successfully submitted, and when I go back to it the field has the date that was previously there.
  2. Ignoring that, although `validDateString()` gets called, and doing `var_dump($this->$attribute)` looks something like `array(1) { ["date"]=> string(0) "18/01/2016" }`, I get "Illegal string offset 'date'" if I try to access `$this->$attribute['date']`. I'm baffled.
  
  **Tags:** plugin-development, validation
  
  > **Comments:**
  > **username_0**: Incidentally, the data coming back as an array with a `"date"` key because I'm having to manage the date fields [like this](http://craftcms.stackexchange.com/questions/8348/how-do-i-save-a-plugins-date-settings-field/8358#8358).  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_0 on 2016-01-18. Score: 2
  
  It turns out that because I'm now using my own settings model, I don't need to use `AttributeType::Mixed` for my date fields any more. (Thanks to carlcs for suggesting this.)
  
  So I've removed the stuff [I had in my Plugin's `getSettingsHtml()`](https://craftcms.stackexchange.com/questions/8348/how-do-i-save-a-plugins-date-settings-field/8358#8358), which massaged the fields before sending them to the template. And now, in theory, I only need to do this:
  
  ```php
  <?php
  namespace Craft;
  
  class My_SettingsModel extends BaseModel
  {
      protected function defineAttributes()
      {
          return array(
              'myText' => array(AttributeType::String,
                              'column' => ColumnType::Text, 'required' => true),
              'myDate' => array(AttributeType::DateTime,
                              'column' => ColumnType::DateTime),
          );
      }
  
      public function rules()
      {
          $rules = parent::rules();
  
          $rules[] = array('myDate', 'date',
                          'format' => array('dd/MM/yyyy'),
                          'allowEmpty' => false,
                          'message' => 'Please enter a valid date');
  
          return $rules;
      }
   }
  
  ```
  
  However, although the form requires me to enter dates - it doesn't accept empty fields - it won't accept the dates I'm typing in. e.g., if I enter `31/12/2016` it fails the date validation. Looking at Yii's [date validation docs](http://www.yiiframework.com/doc/api/1.1/CDateTimeParser) I think this should work...
  
  
  ---
  
  