-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[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
Changes from all commits
271fbfd
483fcf0
212d81f
1b73286
a3fde0f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,8 +22,6 @@ | |
*/ | ||
class TimeValidator extends ConstraintValidator | ||
{ | ||
const PATTERN = '/^(0[0-9]|1[0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])$/'; | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
|
@@ -39,8 +37,35 @@ public function validate($value, Constraint $constraint) | |
|
||
$value = (string) $value; | ||
|
||
if (!preg_match(static::PATTERN, $value)) { | ||
$pattern = $this->getPattern($constraint->withMinutes, $constraint->withSeconds); | ||
|
||
if (!preg_match($pattern, $value)) { | ||
$this->context->addViolation($constraint->message, array('{{ value }}' => $value)); | ||
} | ||
} | ||
|
||
/** | ||
* Returns the regex pattern for validating | ||
* | ||
* @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])"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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"? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
if ($withMinutes) { | ||
// pattern for minutes | ||
$pattern .= "(:([0-5][0-9]))"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do we need the question mark here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yup, mistype. :) There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) { | ||
// because the pattern for seconds is the same as that for minutes, we repeat it twice | ||
$pattern .= "{2}"; | ||
} | ||
} | ||
|
||
return "/^".$pattern."$/"; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Validator\Exception; | ||
|
||
class InvalidConfigurationException extends InvalidArgumentException | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is useless, there is already: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
{ | ||
} |
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.
Should be
with_minutes
.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.
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 10000 .
@stloyd Correct! But if we do so then we must also rename the properties e.g.
protected $with_minutes;
and that's not want we want to do.We need something that converts option names to camelcased strings. How is this handled in other components?
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.
In overall php variables are camelCased, while array keys are written with underscore.
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.
take a look into the base constraint class how options are set:
https://github.com/symfony/symfony/blob/master/src/Symfony/Component/Validator/Constraint.php#L98
if you pass an option named "with_minutes" it's value is not set for the property "withMinutes".
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.
In https://github.com/symfony/symfony/blob/master/src/Symfony/Component/Validator/Constraints/Length.php
The options are first passed to the parents constructor, and then the properties are checked.
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.
Camel casing is fine here.
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.
it should be
=== false
rather than== false
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.
my own comment is not fixed though