8000 [Validator] Fixed time constraint (support formats H, H:i and H:i:s) by bes89 · Pull Request #9163 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Validator] Fixed time constraint (support formats H, H:i and H:i:s) #9163

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

Closed
wants to merge 5 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
added parameter type
  • Loading branch information
bes89 committed Sep 29, 2013
commit a3fde0f38af1277d54976bbb615a2c025d248fd9
10 changes: 4 additions & 6 deletions src/Symfony/Component/Validator/Constraints/TimeValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,22 +47,20 @@ public function validate($value, Constraint $constraint)
/**
* Returns the regex pattern for validating
*
* @param $withMinutes
* @param $withSeconds
* @param Boolean $withMinutes
* @param Boolean $withSeconds
* @return string
*/
protected function getPattern($withMinutes, $withSeconds)
{
// pattern for hours
$pattern = "(0[0-9]|1[0-9]|2[0-3])";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

([01][0-9]|2[0-3])

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hm... single digit hour value like 5:00 is not valid? We could add an option "allow_single_digit"?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(?:[01]?\d|2[0-3])


if ($withMinutes)
{
if ($withMinutes) {
// pattern for minutes
$pattern .= "(:([0-5][0-9]))";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(?::([0-5][0-9]))

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need the question mark here?
With a colon it creates a non-capturing group but the colon must be present, so it must be (?::([0-5][0-9]))

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yup, mistype. :)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moreover, looking at how the regex is captured, I Think every capturing group should be not capturing...


if ($withSeconds)
{
if ($withSeconds) {
// because the pattern for seconds is the same as that for minutes, we repeat it twice
$pattern .= "{2}";
}
Expand Down
3840
0