-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Validator] Added a format option to the DateTime constraint. #17553
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
->setParameter('{{ value }}', $this->formatValue($value)) | ||
->setCode(DateTime::INVALID_DATE_ERROR) | ||
->addViolation(); | ||
} else if ('The parsed time was invalid' === $warning) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
elseif
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 |
3.1.0 | ||
----- | ||
|
||
* deprecated `DateTimeValidator::PATTERN` constant |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
must be added to the upgrade file
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
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
format
option to theDateTime
constraint, this allows to validate dates in custom formats, for example:As you can see this new option allows to use the
DateTime
constraint to validate dates and times, so, maybe theDate
andTime
constraints can be deprecated in this PR.