<p>It sounds like you're on the right track. You could create a custom validator, <a href="https://experiencehq.net/blog/the-definitive-guide-to-validation-in-craft-part-5-custom-validators" rel="nofollow">as detailed here</a>.</p>

<pre class="lang-php prettyprint-override"><code>&lt;?php namespace Craft;

use Carbon\Carbon;

class MyPlugin_ExampleModel extends BaseModel
{
    protected function defineAttributes()
    {
        return [
            'startDate' =&gt; [
                'type'     =&gt; AttributeType::DateTime,
                'required' =&gt; true,
                'compare'  =&gt; '&lt;= endDate',
            ],
            'endDate' =&gt; [
                'type'     =&gt; AttributeType::DateTime,
                'required' =&gt; true,
                'compare'  =&gt; '&gt;= startDate',
            ],
        ];
    }

    public function rules()
    {
        $rules = parent::rules();

        $rules[] = ['startDate', 'validateFutureDate'];
        $rules[] = ['endDate', 'validateFutureDate'];

        return $rules;
    }

    public function validateFutureDate($attribute)
    {
        $value = $this-&gt;$attribute;

        if ($value &amp;&amp; Carbon::now()-&gt;gte($value)) {
            $message = Craft::t("Date must be in the future");
            $this-&gt;addError($attribute, $message);
        }
    }
}
</code></pre>

<p>You can also extract the validation into a separate class, which would make it easier to test (details are in the aforementioned article).</p>

<h2>Caveats and disclaimers</h2>

<p>This code is completely untested. In particular, I'm not sure whether the date comparison will work with the attribute value as-is. You'll probably also want to check what happens to the custom validation if the value contains an invalid date-time string.</p>

CC BY-SA 3.0
2015-06-21T22:19:21.397Z
2015-06-21T22:19:21.397Z
Stephen Lewis
<p>I have a plugin that has a few settings. One of which is a start date and and end date. Everything is set up and working fine. I'd like to validate the start/end dates a a bit though.</p>

<p>I am able to use <code>compare</code> to check that the end isn't before the start, but I'd also like to check that the start (or end) isn't in the past as well.</p>

<p><a href="https://experiencehq.net/blog/the-definitive-guide-to-validation-in-craft-part-3-attribute-rules">Using this awesome page on validation</a> I've seen how to set custom validation via <code>rules</code>. But, since I'm in a settings file, I'm not exactly sure how this translates over.</p>

<p><strong>MyPlugin.php</strong></p>

<pre><code>defineSettings(){
    return array(
        'myStartDate' =&gt; array(
             AttributeType::DateTime,
             'required': true,
             'compare': '&lt;= myEndDate'
         ),
         'myEndDate' =&gt; array(
             AttributeType::DateTime,
             'required': true,
             'compare': '&gt;= myStartDate'
         ),
    )
}
</code></pre>

<p>What I think I'm trying to get is something like this:</p>

<pre><code>....
'myStartDate' =&gt; array(
     AttributeType::DateTime,
     'required': true,
     'something': array(
          'rule': myValidation,
          'message': 'The start date can not be after the finish date.'
      ) 
 )
....

function myValidation() {
    // Do something here
}
</code></pre>

<p>Or, maybe I'm way off.</p>

<plugin-development><datetime><validation>
"Custom" validation for plugin settings page
# "Custom" validation for plugin settings page

(Asked by: username_0 on 2015-04-06)

I have a plugin that has a few settings. One of which is a start date and and end date. Everything is set up and working fine. I'd like to validate the start/end dates a a bit though.
I am able to use compare to check that the end isn't before the start, but I'd also like to check that the start (or end) isn't in the past as well.
Using this awesome page on validation I've seen how to set custom validation via rules. But, since I'm in a settings file, I'm not exactly sure how this translates over.
MyPlugin.php
defineSettings(){
    return array(
        'myStartDate' => array(
             AttributeType::DateTime,
             'required': true,
             'compare': '<= myEndDate'
         ),
         'myEndDate' => array(
             AttributeType::DateTime,
             'required': true,
             'compare': '>= myStartDate'
         ),
    )
}

What I think I'm trying to get is something like this:
....
'myStartDate' => array(
     AttributeType::DateTime,
     'required': true,
     'something': array(
          'rule': myValidation,
          'message': 'The start date can not be after the finish date.'
      ) 
 )
....

function myValidation() {
    // Do something here
}

Or, maybe I'm way off.

**Tags:** plugin-development, datetime, validation

---

## 1 answers

### Answer by username_1 on 2015-06-21. Score: 3

It sounds like you're on the right track. You could create a custom validator, as detailed here.
 [
                'type'     => AttributeType::DateTime,
                'required' => true,
                'compare'  => '<= endDate',
            ],
            'endDate' => [
                'type'     => AttributeType::DateTime,
                'required' => true,
                'compare'  => '>= startDate',
            ],
        ];
    }

    public function rules()
    {
        $rules = parent::rules();

        $rules[] = ['startDate', 'validateFutureDate'];
        $rules[] = ['endDate', 'validateFutureDate'];

        return $rules;
    }

    public function validateFutureDate($attribute)
    {
        $value = $this->$attribute;

        if ($value && Carbon::now()->gte($value)) {
            $message = Craft::t("Date must be in the future");
            $this->addError($attribute, $message);
        }
    }
}

You can also extract the validation into a separate class, which would make it easier to test (details are in the aforementioned article).
Caveats and disclaimers
This code is completely untested. In particular, I'm not sure whether the date comparison will work with the attribute value as-is. You'll probably also want to check what happens to the custom validation if the value contains an invalid date-time string.


---

