bug #11559 [Validator] Convert objects to string in comparison validators (webmozart)
This PR was merged into the 2.3 branch. Discussion ---------- [Validator] Convert objects to string in comparison validators | Q | A | ------------- | --- | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | - | License | MIT | Doc PR | - In the [latest merge from 2.3 into 2.4](/symfony/symfony/commit/3bed1b7988e94a897a64c6a2ad3bf70bde9005c1), the changes from6cf5e0812e
in 2.4 got lost. This PR brings back these changes and backports them to 2.3. The change is BC, because the former value `true` of the `$prettyDateTime` will be cast to `1`, which corresponds to the `PRETTY_DATE` format constant. Commits -------273671e
[Validator] Convert objects to string in comparison validators. Reapplies6cf5e0812e
This commit is contained in:
commit
6b9ce52a11
@ -20,6 +20,21 @@ namespace Symfony\Component\Validator;
|
|||||||
*/
|
*/
|
||||||
abstract class ConstraintValidator implements ConstraintValidatorInterface
|
abstract class ConstraintValidator implements ConstraintValidatorInterface
|
||||||
{
|
{
|
||||||
|
/**
|
||||||
|
* Whether to format {@link \DateTime} objects as RFC-3339 dates
|
||||||
|
* ("Y-m-d H:i:s").
|
||||||
|
*
|
||||||
|
* @var integer
|
||||||
|
*/
|
||||||
|
const PRETTY_DATE = 1;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Whether to cast objects with a "__toString()" method to strings.
|
||||||
|
*
|
||||||
|
* @var integer
|
||||||
|
*/
|
||||||
|
const OBJECT_TO_STRING = 2;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var ExecutionContextInterface
|
* @var ExecutionContextInterface
|
||||||
*/
|
*/
|
||||||
@ -66,15 +81,15 @@ abstract class ConstraintValidator implements ConstraintValidatorInterface
|
|||||||
* won't know what an "object", "array" or "resource" is and will be
|
* won't know what an "object", "array" or "resource" is and will be
|
||||||
* confused by the violation message.
|
* confused by the violation message.
|
||||||
*
|
*
|
||||||
* @param mixed $value The value to format as string
|
* @param mixed $value The value to format as string
|
||||||
* @param bool $prettyDateTime Whether to format {@link \DateTime}
|
* @param integer $format A bitwise combination of the format
|
||||||
* objects as RFC-3339 dates ("Y-m-d H:i:s")
|
* constants in this class
|
||||||
*
|
*
|
||||||
* @return string The string representation of the passed value
|
* @return string The string representation of the passed value
|
||||||
*/
|
*/
|
||||||
protected function formatValue($value, $prettyDateTime = false)
|
protected function formatValue($value, $format = 0)
|
||||||
{
|
{
|
||||||
if ($prettyDateTime && $value instanceof \DateTime) {
|
if (($format & self::PRETTY_DATE) && $value instanceof \DateTime) {
|
||||||
if (class_exists('IntlDateFormatter')) {
|
if (class_exists('IntlDateFormatter')) {
|
||||||
$locale = \Locale::getDefault();
|
$locale = \Locale::getDefault();
|
||||||
$formatter = new \IntlDateFormatter($locale, \IntlDateFormatter::MEDIUM, \IntlDateFormatter::SHORT);
|
$formatter = new \IntlDateFormatter($locale, \IntlDateFormatter::MEDIUM, \IntlDateFormatter::SHORT);
|
||||||
@ -86,6 +101,10 @@ abstract class ConstraintValidator implements ConstraintValidatorInterface
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (is_object($value)) {
|
if (is_object($value)) {
|
||||||
|
if ($format & self::OBJECT_TO_STRING && method_exists($value, '__toString')) {
|
||||||
|
return $value->__toString();
|
||||||
|
}
|
||||||
|
|
||||||
return 'object';
|
return 'object';
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -122,18 +141,18 @@ abstract class ConstraintValidator implements ConstraintValidatorInterface
|
|||||||
* Each of the values is converted to a string using
|
* Each of the values is converted to a string using
|
||||||
* {@link formatValue()}. The values are then concatenated with commas.
|
* {@link formatValue()}. The values are then concatenated with commas.
|
||||||
*
|
*
|
||||||
* @param array $values A list of values
|
* @param array $values A list of values
|
||||||
* @param bool $prettyDateTime Whether to format {@link \DateTime}
|
* @param integer $format A bitwise combination of the format
|
||||||
* objects as RFC-3339 dates ("Y-m-d H:i:s")
|
* constants in this class
|
||||||
*
|
*
|
||||||
* @return string The string representation of the value list
|
* @return string The string representation of the value list
|
||||||
*
|
*
|
||||||
* @see formatValue()
|
* @see formatValue()
|
||||||
*/
|
*/
|
||||||
protected function formatValues(array $values, $prettyDateTime = false)
|
protected function formatValues(array $values, $format = 0)
|
||||||
{
|
{
|
||||||
foreach ($values as $key => $value) {
|
foreach ($values as $key => $value) {
|
||||||
$values[$key] = $this->formatValue($value, $prettyDateTime);
|
$values[$key] = $this->formatValue($value, $format);
|
||||||
}
|
}
|
||||||
|
|
||||||
return implode(', ', $values);
|
return implode(', ', $values);
|
||||||
|
@ -32,8 +32,8 @@ abstract class AbstractComparisonValidator extends ConstraintValidator
|
|||||||
|
|
||||||
if (!$this->compareValues($value, $constraint->value)) {
|
if (!$this->compareValues($value, $constraint->value)) {
|
||||||
$this->context->addViolation($constraint->message, array(
|
$this->context->addViolation($constraint->message, array(
|
||||||
'{{ value }}' => $this->formatValue($value, true),
|
'{{ value }}' => $this->formatValue($value, self::OBJECT_TO_STRING | self::PRETTY_DATE),
|
||||||
'{{ compared_value }}' => $this->formatValue($constraint->value, true),
|
'{{ compared_value }}' => $this->formatValue($constraint->value, self::OBJECT_TO_STRING | self::PRETTY_DATE),
|
||||||
'{{ compared_value_type }}' => $this->formatTypeOf($constraint->value)
|
'{{ compared_value_type }}' => $this->formatTypeOf($constraint->value)
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
@ -15,6 +15,21 @@ use Symfony\Component\Intl\Util\IntlTestHelper;
|
|||||||
use Symfony\Component\Validator\Constraint;
|
use Symfony\Component\Validator\Constraint;
|
||||||
use Symfony\Component\Validator\Constraints\AbstractComparisonValidator;
|
use Symfony\Component\Validator\Constraints\AbstractComparisonValidator;
|
||||||
|
|
||||||
|
class ComparisonTest_Class
|
||||||
|
{
|
||||||
|
protected $value;
|
||||||
|
|
||||||
|
public function __construct($value)
|
||||||
|
{
|
||||||
|
$this->value = $value;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function __toString()
|
||||||
|
{
|
||||||
|
return (string) $this->value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Daniel Holmes <daniel@danielholmes.org>
|
* @author Daniel Holmes <daniel@danielholmes.org>
|
||||||
*/
|
*/
|
||||||
|
@ -39,6 +39,7 @@ class EqualToValidatorTest extends AbstractComparisonValidatorTestCase
|
|||||||
array(3, '3'),
|
array(3, '3'),
|
||||||
array('a', 'a'),
|
array('a', 'a'),
|
||||||
array(new \DateTime('2000-01-01'), new \DateTime('2000-01-01')),
|
array(new \DateTime('2000-01-01'), new \DateTime('2000-01-01')),
|
||||||
|
array(new ComparisonTest_Class(5), new ComparisonTest_Class(5)),
|
||||||
array(null, 1),
|
array(null, 1),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@ -51,7 +52,8 @@ class EqualToValidatorTest extends AbstractComparisonValidatorTestCase
|
|||||||
return array(
|
return array(
|
||||||
array(1, '1', 2, '2', 'integer'),
|
array(1, '1', 2, '2', 'integer'),
|
||||||
array('22', '"22"', '333', '"333"', 'string'),
|
array('22', '"22"', '333', '"333"', 'string'),
|
||||||
array(new \DateTime('2001-01-01'), 'Jan 1, 2001, 12:00 AM', new \DateTime('2000-01-01'), 'Jan 1, 2000, 12:00 AM', 'DateTime')
|
array(new \DateTime('2001-01-01'), 'Jan 1, 2001, 12:00 AM', new \DateTime('2000-01-01'), 'Jan 1, 2000, 12:00 AM', 'DateTime'),
|
||||||
|
array(new ComparisonTest_Class(4), '4', new ComparisonTest_Class(5), '5', __NAMESPACE__.'\ComparisonTest_Class'),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -37,6 +37,7 @@ class GreaterThanValidatorTest extends AbstractComparisonValidatorTestCase
|
|||||||
return array(
|
return array(
|
||||||
array(2, 1),
|
array(2, 1),
|
||||||
array(new \DateTime('2005/01/01'), new \DateTime('2001/01/01')),
|
array(new \DateTime('2005/01/01'), new \DateTime('2001/01/01')),
|
||||||
|
array(new ComparisonTest_Class(5), new ComparisonTest_Class(4)),
|
||||||
array('333', '22'),
|
array('333', '22'),
|
||||||
array(null, 1),
|
array(null, 1),
|
||||||
);
|
);
|
||||||
@ -52,6 +53,8 @@ class GreaterThanValidatorTest extends AbstractComparisonValidatorTestCase
|
|||||||
array(2, '2', 2, '2', 'integer'),
|
array(2, '2', 2, '2', 'integer'),
|
||||||
array(new \DateTime('2000/01/01'), 'Jan 1, 2000, 12:00 AM', new \DateTime('2005/01/01'), 'Jan 1, 2005, 12:00 AM', 'DateTime'),
|
array(new \DateTime('2000/01/01'), 'Jan 1, 2000, 12:00 AM', new \DateTime('2005/01/01'), 'Jan 1, 2005, 12:00 AM', 'DateTime'),
|
||||||
array(new \DateTime('2000/01/01'), 'Jan 1, 2000, 12:00 AM', new \DateTime('2000/01/01'), 'Jan 1, 2000, 12:00 AM', 'DateTime'),
|
array(new \DateTime('2000/01/01'), 'Jan 1, 2000, 12:00 AM', new \DateTime('2000/01/01'), 'Jan 1, 2000, 12:00 AM', 'DateTime'),
|
||||||
|
array(new ComparisonTest_Class(4), '4', new ComparisonTest_Class(5), '5', __NAMESPACE__.'\ComparisonTest_Class'),
|
||||||
|
array(new ComparisonTest_Class(5), '5', new ComparisonTest_Class(5), '5', __NAMESPACE__.'\ComparisonTest_Class'),
|
||||||
array('22', '"22"', '333', '"333"', 'string'),
|
array('22', '"22"', '333', '"333"', 'string'),
|
||||||
array('22', '"22"', '22', '"22"', 'string')
|
array('22', '"22"', '22', '"22"', 'string')
|
||||||
);
|
);
|
||||||
|
@ -35,11 +35,13 @@ class IdenticalToValidatorTest extends AbstractComparisonValidatorTestCase
|
|||||||
public function provideValidComparisons()
|
public function provideValidComparisons()
|
||||||
{
|
{
|
||||||
$date = new \DateTime('2000-01-01');
|
$date = new \DateTime('2000-01-01');
|
||||||
|
$object = new ComparisonTest_Class(2);
|
||||||
|
|
||||||
return array(
|
return array(
|
||||||
array(3, 3),
|
array(3, 3),
|
||||||
array('a', 'a'),
|
array('a', 'a'),
|
||||||
array($date, $date),
|
array($date, $date),
|
||||||
|
array($object, $object),
|
||||||
array(null, 1),
|
array(null, 1),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@ -54,7 +56,8 @@ class IdenticalToValidatorTest extends AbstractComparisonValidatorTestCase
|
|||||||
array(2, '2', '2', '"2"', 'string'),
|
array(2, '2', '2', '"2"', 'string'),
|
||||||
array('22', '"22"', '333', '"333"', 'string'),
|
array('22', '"22"', '333', '"333"', 'string'),
|
||||||
array(new \DateTime('2001-01-01'), 'Jan 1, 2001, 12:00 AM', new \DateTime('2001-01-01'), 'Jan 1, 2001, 12:00 AM', 'DateTime'),
|
array(new \DateTime('2001-01-01'), 'Jan 1, 2001, 12:00 AM', new \DateTime('2001-01-01'), 'Jan 1, 2001, 12:00 AM', 'DateTime'),
|
||||||
array(new \DateTime('2001-01-01'), 'Jan 1, 2001, 12:00 AM', new \DateTime('1999-01-01'), 'Jan 1, 1999, 12:00 AM', 'DateTime')
|
array(new \DateTime('2001-01-01'), 'Jan 1, 2001, 12:00 AM', new \DateTime('1999-01-01'), 'Jan 1, 1999, 12:00 AM', 'DateTime'),
|
||||||
|
array(new ComparisonTest_Class(4), '4', new ComparisonTest_Class(5), '5', __NAMESPACE__.'\ComparisonTest_Class'),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -39,6 +39,8 @@ class LessThanOrEqualValidatorTest extends AbstractComparisonValidatorTestCase
|
|||||||
array(1, 1),
|
array(1, 1),
|
||||||
array(new \DateTime('2000-01-01'), new \DateTime('2000-01-01')),
|
array(new \DateTime('2000-01-01'), new \DateTime('2000-01-01')),
|
||||||
array(new \DateTime('2000-01-01'), new \DateTime('2020-01-01')),
|
array(new \DateTime('2000-01-01'), new \DateTime('2020-01-01')),
|
||||||
|
array(new ComparisonTest_Class(4), new ComparisonTest_Class(5)),
|
||||||
|
array(new ComparisonTest_Class(5), new ComparisonTest_Class(5)),
|
||||||
array('a', 'a'),
|
array('a', 'a'),
|
||||||
array('a', 'z'),
|
array('a', 'z'),
|
||||||
array(null, 1),
|
array(null, 1),
|
||||||
@ -53,6 +55,7 @@ class LessThanOrEqualValidatorTest extends AbstractComparisonValidatorTestCase
|
|||||||
return array(
|
return array(
|
||||||
array(2, '2', 1, '1', 'integer'),
|
array(2, '2', 1, '1', 'integer'),
|
||||||
array(new \DateTime('2010-01-01'), 'Jan 1, 2010, 12:00 AM', new \DateTime('2000-01-01'), 'Jan 1, 2000, 12:00 AM', 'DateTime'),
|
array(new \DateTime('2010-01-01'), 'Jan 1, 2010, 12:00 AM', new \DateTime('2000-01-01'), 'Jan 1, 2000, 12:00 AM', 'DateTime'),
|
||||||
|
array(new ComparisonTest_Class(5), '5', new ComparisonTest_Class(4), '4', __NAMESPACE__.'\ComparisonTest_Class'),
|
||||||
array('c', '"c"', 'b', '"b"', 'string')
|
array('c', '"c"', 'b', '"b"', 'string')
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -37,6 +37,7 @@ class LessThanValidatorTest extends AbstractComparisonValidatorTestCase
|
|||||||
return array(
|
return array(
|
||||||
array(1, 2),
|
array(1, 2),
|
||||||
array(new \DateTime('2000-01-01'), new \DateTime('2010-01-01')),
|
array(new \DateTime('2000-01-01'), new \DateTime('2010-01-01')),
|
||||||
|
array(new ComparisonTest_Class(4), new ComparisonTest_Class(5)),
|
||||||
array('22', '333'),
|
array('22', '333'),
|
||||||
array(null, 1),
|
array(null, 1),
|
||||||
);
|
);
|
||||||
@ -52,6 +53,8 @@ class LessThanValidatorTest extends AbstractComparisonValidatorTestCase
|
|||||||
array(2, '2', 2, '2', 'integer'),
|
array(2, '2', 2, '2', 'integer'),
|
||||||
array(new \DateTime('2010-01-01'), 'Jan 1, 2010, 12:00 AM', new \DateTime('2000-01-01'), 'Jan 1, 2000, 12:00 AM', 'DateTime'),
|
array(new \DateTime('2010-01-01'), 'Jan 1, 2010, 12:00 AM', new \DateTime('2000-01-01'), 'Jan 1, 2000, 12:00 AM', 'DateTime'),
|
||||||
array(new \DateTime('2000-01-01'), 'Jan 1, 2000, 12:00 AM', new \DateTime('2000-01-01'), 'Jan 1, 2000, 12:00 AM', 'DateTime'),
|
array(new \DateTime('2000-01-01'), 'Jan 1, 2000, 12:00 AM', new \DateTime('2000-01-01'), 'Jan 1, 2000, 12:00 AM', 'DateTime'),
|
||||||
|
array(new ComparisonTest_Class(5), '5', new ComparisonTest_Class(5), '5', __NAMESPACE__.'\ComparisonTest_Class'),
|
||||||
|
array(new ComparisonTest_Class(6), '6', new ComparisonTest_Class(5), '5', __NAMESPACE__.'\ComparisonTest_Class'),
|
||||||
array('333', '"333"', '22', '"22"', 'string'),
|
array('333', '"333"', '22', '"22"', 'string'),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -38,6 +38,7 @@ class NotEqualToValidatorTest extends AbstractComparisonValidatorTestCase
|
|||||||
array(1, 2),
|
array(1, 2),
|
||||||
array('22', '333'),
|
array('22', '333'),
|
||||||
array(new \DateTime('2001-01-01'), new \DateTime('2000-01-01')),
|
array(new \DateTime('2001-01-01'), new \DateTime('2000-01-01')),
|
||||||
|
array(new ComparisonTest_Class(6), new ComparisonTest_Class(5)),
|
||||||
array(null, 1),
|
array(null, 1),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@ -51,7 +52,8 @@ class NotEqualToValidatorTest extends AbstractComparisonValidatorTestCase
|
|||||||
array(3, '3', 3, '3', 'integer'),
|
array(3, '3', 3, '3', 'integer'),
|
||||||
array('2', '"2"', 2, '2', 'integer'),
|
array('2', '"2"', 2, '2', 'integer'),
|
||||||
array('a', '"a"', 'a', '"a"', 'string'),
|
array('a', '"a"', 'a', '"a"', 'string'),
|
||||||
array(new \DateTime('2000-01-01'), 'Jan 1, 2000, 12:00 AM', new \DateTime('2000-01-01'), 'Jan 1, 2000, 12:00 AM', 'DateTime')
|
array(new \DateTime('2000-01-01'), 'Jan 1, 2000, 12:00 AM', new \DateTime('2000-01-01'), 'Jan 1, 2000, 12:00 AM', 'DateTime'),
|
||||||
|
array(new ComparisonTest_Class(5), '5', new ComparisonTest_Class(5), '5', __NAMESPACE__.'\ComparisonTest_Class'),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -50,11 +50,13 @@ class NotIdenticalToValidatorTest extends AbstractComparisonValidatorTestCase
|
|||||||
public function provideInvalidComparisons()
|
public function provideInvalidComparisons()
|
||||||
{
|
{
|
||||||
$date = new \DateTime('2000-01-01');
|
$date = new \DateTime('2000-01-01');
|
||||||
|
$object = new ComparisonTest_Class(2);
|
||||||
|
|
||||||
return array(
|
return array(
|
||||||
array(3, '3', 3, '3', 'integer'),
|
array(3, '3', 3, '3', 'integer'),
|
||||||
array('a', '"a"', 'a', '"a"', 'string'),
|
array('a', '"a"', 'a', '"a"', 'string'),
|
||||||
array($date, 'Jan 1, 2000, 12:00 AM', $date, 'Jan 1, 2000, 12:00 AM', 'DateTime')
|
array($date, 'Jan 1, 2000, 12:00 AM', $date, 'Jan 1, 2000, 12:00 AM', 'DateTime'),
|
||||||
|
array($object, '2', $object, '2', __NAMESPACE__.'\ComparisonTest_Class'),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user