8000 [Validator] Fixed time constraint (support formats H, H:i and H:i:s) by bes89 · Pull Request #11821 · 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) #11821

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 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
16 changes: 16 additions & 0 deletions src/Symfony/Component/Validator/Constraints/Time.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\Component\Validator\Constraints;

use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\Exception\InvalidConfigurationException;

/**
* @Annotation
Expand All @@ -24,4 +25,19 @@
class Time extends Constraint
{
public $message = 'This value is not a valid time.';
public $withMinutes = true;
public $withSeconds = true;

public function __construct($options = null)
{
parent::__construct($options);

if (isset($options['withMinutes']) && !isset($options['withSeconds']) && $options['withMinutes'] === false) {
$this->withSeconds = false;
}

if ($this->withSeconds && !$this->withMinutes) {
throw new InvalidConfigurationException('You can not disable minutes if you have enabled seconds.');
}
}
}
35 changes: 29 additions & 6 deletions src/Symfony/Component/Validator/Constraints/TimeValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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}
*/
Expand All @@ -43,10 +41,35 @@ public function validate($value, Constraint $constraint)

$value = (string) $value;

if (!preg_match(static::PATTERN, $value)) {
$this->context->addViolation($constraint->message, array(
'{{ value }}' => $this->formatValue($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 bool $withMinutes
* @param bool $withSeconds
* @return string
*/
protected function getPattern($withMinutes, $withSeconds)
{
// pattern for hours
$pattern = "(0?[0-9]|1[0-9]|2[0-3])";

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

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
Copy link
Contributor

Choose a reason for hiding this comment

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

Copy link
Contributor

Choose a reason for hiding this comment

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

{
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Symfony\Component\Validator\Constraints\Time;
use Symfony\Component\Validator\Constraints\TimeValidator;
use Symfony\Component\Validator\Validation;
use Symfony\Component\Validator\Tests\Fixtures\InvalidConstraint;

class TimeValidatorTest extends AbstractConstraintValidatorTest
{
Expand All @@ -27,6 +28,17 @@ protected function createValidator()
return new TimeValidator();
}

/**
* @expectedException \Symfony\Component\Validator\Exception\InvalidConfigurationException
*/
public function testThrowsExceptionIfConfigurationIsInvalid()
{
new Time(array(
'withMinutes' => false,
'withSeconds' => true,
));
}

public function testNullIsValid()
{
$this->validator->validate(null, new Time());
Expand Down Expand Up @@ -75,6 +87,51 @@ public function getValidTimes()
);
}

/**
* @dataProvider getValidTimesWithoutMinutes
*/
public function testValidTimesWithoutMinutes($time)
{
$this->validator->validate($time, new Time(array(
'withMinutes' => false,
)));

$this->assertNoViolation();
}

public function getValidTimesWithoutMinutes()
{
return array(
array('01'),
array('00'),
array('0'),
array('23'),
array('5'),
);
}

/**
* @dataProvider getValidTimesWithoutSeconds
*/
public function testValidTimesWithoutSeconds($time)
{
$this->validator->validate($time, new Time(array(
'withSeconds' => false,
)));

$this->assertNoViolation();
}

public function getValidTimesWithoutSeconds()
{
return array(
array('01:02'),
array('00:00'),
array('4:00'),
array('23:59'),
);
}

/**
* @dataProvider getInvalidTimes
*/
Expand All @@ -87,7 +144,7 @@ public function testInvalidTimes($time)
$this->validator->validate($time, $constraint);

$this->assertViolation('myMessage', array(
'{{ value }}' => '"'.$time.'"',
'{{ value }}' => $time,
));
}

Expand All @@ -98,9 +155,67 @@ public function getInvalidTimes()
array('foobar 12:34:56'),
array('12:34:56 foobar'),
array('00:00'),
array('05:3'),
array('24:00:00'),
array('00:60:00'),
array('00:00:60'),
);
}

/**
* @dataProvider getInvalidTimesWithoutMinutes
*/
public function testInvalidTimesWithoutMinutes($time)
{
$constraint = new Time(array(
'message' => 'myMessage',
'withMinutes' => false,
));

$this->validator->validate($time, $constraint);

$this->assertViolation('myMessage', array(
'{{ value }}' => $time,
));
}

public function getInvalidTimesWithoutMinutes()
{
return array(
array('foobar'),
array('foobar 12'),
array('12 foobar'),
array('24'),
array('60'),
);
}

/**
* @dataProvider getInvalidTimesWithoutSeconds
*/
public function testInvalidTimesWithoutSeconds($time)
{
$constraint = new Time(array(
'message' => 'myMessage',
'withSeconds' => false,
));

$this->validator->validate($time, $constraint);

$this->assertViolation('myMessage', array(
'{{ value }}' => $time,
));
}

public function getInvalidTimesWithoutSeconds()
{
return array(
array('foobar'),
array('foobar 12:34'),
array('12:34 foobar'),
array('01:02:03'),
array('24:00'),
array('00:60'),
);
}
}
0