yiisoft / yii2

Yii 2: The Fast, Secure and Professional PHP Framework
http://www.yiiframework.com
BSD 3-Clause "New" or "Revised" License
14.24k stars 6.91k forks source link

`DateValidator` should check if date is already in `DateValidator::$timestampAttributeFormat` #8602

Closed stevekr closed 8 years ago

stevekr commented 9 years ago

I have form field in custom date format that is converted to mysql format:

['some_date', 'date', 'format' => 'dd.MM.yyyy', 'timestampAttribute' => 'some_date', 'timestampAttributeFormat' => 'php:Y-m-d']

Saving with form works great, but if I need to update some other field without using form and try to validate, it gives me an invalid format error, because some_date is not converted to dd.MM.yyyy format and is still in php:Y-m-d. So attributes in DateValidator::$timestampAttributeFormat format should also be valid.

klimov-paul commented 9 years ago

Such request is not implemenatble: if DateValidator will fill timestampAttribute only if it is not set, it will be unable to update its value anyhow. You should either use a scenarios to separate validation of user input and internal one or create a separated field some_date_source, which should be used in user form and care the DateValidator.

stevekr commented 9 years ago

Maybe I have completely missed the point of timestampAttributeFormat, but I see it as a way to ensure date fields are always in valid format for db (php:Y-m-d). So if user submits form with date in dd.MM.yyyy format, it gets converted and all is good. If date is loaded from db and is already in valid php:Y-m-d format, then it should not give validation errors. So it seems logical that DateValidator uses both format and timestampAttributeFormat for validation.

armpogart commented 9 years ago

This issue may be also fixed if validation is triggered only on dirty attributes #8610

klimov-paul commented 8 years ago

I suppose this could be implemented.

stevekr commented 8 years ago

Thanks :thumbsup:

klimov-paul commented 8 years ago

Resolved by commit 779b1e90cea13ca3a05768a8e82b96b022d1f710