From 7bfb8c1a4c8cb993028ad8ef909c9f6efceb36b6 Mon Sep 17 00:00:00 2001 From: Valentin Udaltsov Date: Thu, 28 Mar 2019 02:59:01 +0300 Subject: [PATCH] [Validator] Fix annotation default for @Count and @Length --- src/Symfony/Component/Validator/Constraints/Count.php | 3 +++ .../Component/Validator/Constraints/Length.php | 3 +++ .../Tests/Constraints/CountValidatorTest.php | 9 +++++++++ .../Tests/Constraints/LengthValidatorTest.php | 11 ++++++++++- 4 files changed, 25 insertions(+), 1 deletion(-) diff --git a/src/Symfony/Component/Validator/Constraints/Count.php b/src/Symfony/Component/Validator/Constraints/Count.php index 8de10edfc880..b11f994f58b6 100644 --- a/src/Symfony/Component/Validator/Constraints/Count.php +++ b/src/Symfony/Component/Validator/Constraints/Count.php @@ -43,6 +43,9 @@ public function __construct($options = null) 'min' => $options, 'max' => $options, ]; + } elseif (\is_array($options) && isset($options['value']) && !isset($options['min']) && !isset($options['max'])) { + $options['min'] = $options['max'] = $options['value']; + unset($options['value']); } parent::__construct($options); diff --git a/src/Symfony/Component/Validator/Constraints/Length.php b/src/Symfony/Component/Validator/Constraints/Length.php index 79aa473204ea..996a1e479f45 100644 --- a/src/Symfony/Component/Validator/Constraints/Length.php +++ b/src/Symfony/Component/Validator/Constraints/Length.php @@ -47,6 +47,9 @@ public function __construct($options = null) 'min' => $options, 'max' => $options, ]; + } elseif (\is_array($options) && isset($options['value']) && !isset($options['min']) && !isset($options['max'])) { + $options['min'] = $options['max'] = $options['value']; + unset($options['value']); } parent::__construct($options); diff --git a/src/Symfony/Component/Validator/Tests/Constraints/CountValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/CountValidatorTest.php index 8416136fd4af..01e23cc3b92c 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/CountValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/CountValidatorTest.php @@ -195,4 +195,13 @@ public function testDefaultOption() $this->assertEquals(5, $constraint->min); $this->assertEquals(5, $constraint->max); } + + public function testConstraintAnnotationDefaultOption() + { + $constraint = new Count(['value' => 5, 'exactMessage' => 'message']); + + $this->assertEquals(5, $constraint->min); + $this->assertEquals(5, $constraint->max); + $this->assertEquals('message', $constraint->exactMessage); + } } diff --git a/src/Symfony/Component/Validator/Tests/Constraints/LengthValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/LengthValidatorTest.php index 955d8b556453..f1daee534aa3 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/LengthValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/LengthValidatorTest.php @@ -237,11 +237,20 @@ public function testOneCharset($value, $charset, $isValid) } } - public function testConstraintGetDefaultOption() + public function testConstraintDefaultOption() { $constraint = new Length(5); $this->assertEquals(5, $constraint->min); $this->assertEquals(5, $constraint->max); } + + public function testConstraintAnnotationDefaultOption() + { + $constraint = new Length(['value' => 5, 'exactMessage' => 'message']); + + $this->assertEquals(5, $constraint->min); + $this->assertEquals(5, $constraint->max); + $this->assertEquals('message', $constraint->exactMessage); + } }