[Validator] Added a format option to the DateTime constraint.#17553
Conversation
|
I'm 👍 for this addition, but I made several comments about the implementation. |
|
@fabpot Comments addressed |
|
The failing tests seems to not be related. Fatal error: Call to undefined method Symfony\Component\DependencyInjection\Definition::getParent() in /home/travis/build/symfony/symfony/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/FrameworkExtensionTest.php on line 535 |
There was a problem hiding this comment.
must be added to the upgrade file
|
Can we add a check to the constraint that ensures that the format string is something that is understood by |
|
@dosten Can you make the missing changes? |
|
@xabbuh I'll work on this ASAP |
|
@xabbuh The |
|
Thanks for that feature 👍 |
|
Some feedback? @xabbuh @webmozart @fabpot |
|
@dosten But wouldn't be better to use a different error code if the format string cannot be parsed? |
|
@xabbuh can you give an example of a "unparsable" format? |
|
@dosten Take this example: $date = \DateTime::createFromFormat('f.m.Y', '27.02.2016');
|
|
@dosten Any news on this one? |
|
@fabpot @xabbuh The "f" format char does not exists, so your code is expecting a format like: "f.02.2016": $date = \DateTime::createFromFormat('f.m.Y', 'f.02.2016');
If you pass "27.02.2016" the validator will add a INVALID_FORMAT_ERROR violation, IMO this is correct. |
|
Ah yes, you are right. I missed that you have to use these characters if they are present in the user input. Sorry for the confusion. 👍 Status: Reviewed |
|
Thank you @dosten. |
…traint. (dosten) This PR was merged into the 3.1-dev branch. Discussion ---------- [Validator] Added a format option to the DateTime constraint. | Q | A | ------------- | --- | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | yes | Tests pass? | yes | Fixed tickets | #14521 | License | MIT This PR adds a `format` option to the `DateTime` constraint, this allows to validate dates in custom formats, for example: ```php use Symfony\Component\Validator\Constraints\DateTime; use Symfony\Component\Validator\Validation; $validator = Validation::createValidator(); $validator->validate('December 31, 1999', new DateTime(['format' => 'F d, Y'])); $validator->validate('01:02:03', new DateTime(['format' => 'H:i:s'])); $validator->validate('2010/01/01 01:02', new DateTime(['format' => 'Y/m/d H:i'])); ``` As you can see this new option allows to use the `DateTime` constraint to validate dates and times, so, maybe the `Date` and `Time` constraints can be deprecated in this PR. Commits ------- 9e94c9f Added a format option to the DateTime constraint.
|
@dosten Can you submit a PR on symfony/symfony-docs to document this new feature? Thanks. |
|
@dosten You can refer to symfony/symfony-docs#6313 (I created it so that we do not forget about this feature). |
This PR was merged into the master branch. Discussion ---------- [Validator] Added docs about the format option | Q | A | ------------- | --- | Doc fix? | no | New docs? | yes, see symfony/symfony#17553 | Applies to | 3.1+ | Fixed tickets | #6313 Commits ------- f8d1e82 Added docs about the format option
|
Nice feature, thanks! |
This PR adds a
formatoption to theDateTimeconstraint, this allows to validate dates in custom formats, for example:As you can see this new option allows to use the
DateTimeconstraint to validate dates and times, so, maybe theDateandTimeconstraints can be deprecated in this PR.