Mark Johnson Mark Johnson - 3 years ago 365
PHP Question

Yii2 DateTime model validation format issue

Created a model with a dateTime field called 'until', the name is not important just that it is a date time.
Here is it's validation in the rules of the model.

['until', 'datetime', 'format' => 'Y-m-d H:i:s'],


Later after I instantiate the model and update the field I get a validation error.

$model->until = '2017-10-15 14:30:04';
$eventRepeat->validate();
$errors = $model->errors;


$errors['until'][0] will then be 'The format of Until is invalid.'

I was not expecting this error.

Answer Source

As in Yii2 Doc

format: the date/time format that the value being validated should be in. This can be a date time pattern as described in the ICU manual. Alternatively this can be a string prefixed with php: representing a format that can be recognized by the PHP Datetime class.

http://www.yiiframework.com/doc-2.0/guide-tutorial-core-validators.html#date

try using

['until', 'datetime', 'format' => 'php:Y-m-d H:i:s'],
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download