Reputation: 645
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.
Upvotes: 0
Views: 7731
Reputation: 645
Solved!
set the format to
php:Y-m-d H:i:s
and used the formatter.
$model->until = Yii::$app
->formatter
->asDatetime(
new DateTime('NOW + 2 weeks'),
'php:Y-m-d H:i:s'
);
Upvotes: 2
Reputation: 133370
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'],
Upvotes: 6