1

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.

<?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.
  • Incidentally, the data coming back as an array with a "date" key because I'm having to manage the date fields like this. – Phil Gyford Jan 18 '16 at 11:05
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(), which massaged the fields before sending them to the template. And now, in theory, I only need to do this:

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

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.