Validate date field

Hi there,

my form has a datefield (format d.m.Y) and I want to make sure, that the entered date is >= today (the storage format of the date is Y-m-D).

As I read the validator documentation, I saw this passage:

"Lower limit of the date. Defaults to null, meaning no lower limit. This can be a unix timestamp or a string representing a date time value. If this property is a string, $format will be used to parse it.

See also $tooSmall for the customized message used when the date is too small."

https://www.yiiframework.com/doc/api/2.0/yii-validators-datevalidator#$min-detail

But I dont get it to work. Does anyone have a working example?

Thanks in advance!

There are many ways you can use non-date validators and docs on validation is pretty solid. Here is an example using inline validator


namespace app\models;


use Yii; 


class YourModel extends Model

{

	public $date;

	//... other fields


	public function rules()

	{

    	return [

        	//... other validation rules here

        	['date', function ($attribute, $params, $validator){

            	//do your validation using Date class

            	$modelDate = new \DateTime($this->date);

            	$nowDate = new  \DateTime();

            	if($modelDate->diff($nowDate) >0 )

                	$validator->addError($this, $attribute, Yii::t('app', 'Date is inccorect')); //whatever message appropriate

        	}],

    	];

	}

}