Validating date fields in a plugin's settings 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 the date attributes are set as AttributeType::Mixed.

I'm using a structure like this and this 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.

&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');
         }
     }
 }


myText is required OK - the form re-displays with an error message if you try to leave it empty.

I have two problems:


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.
Ignoring that, although validDateString() gets called, and doing var_dump($this-&gt;$attribute) looks something like array(1) { ["date"]=&gt; string(0) "18/01/2016" }, I get "Illegal string offset 'date'" if I try to access $this-&gt;$attribute['date']. I'm baffled.
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(), which massaged the fields before sending them to the template. And now, in theory, I only need to do this:

&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;
    }
 }


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 I think this should work...