From 76e3ef19efff2b9965ff2b6c2f653b797917e127 Mon Sep 17 00:00:00 2001 From: Peter Rehm Date: Fri, 1 Jul 2016 09:59:41 +0200 Subject: [PATCH 1/5] Make strict the default option for choice validation --- src/Symfony/Component/Validator/Constraints/Choice.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Symfony/Component/Validator/Constraints/Choice.php b/src/Symfony/Component/Validator/Constraints/Choice.php index 4b93c70e4a5f4..8c4c23ad0ed65 100644 --- a/src/Symfony/Component/Validator/Constraints/Choice.php +++ b/src/Symfony/Component/Validator/Constraints/Choice.php @@ -34,7 +34,7 @@ class Choice extends Constraint public $choices; public $callback; public $multiple = false; - public $strict = false; + public $strict = true; public $min; public $max; public $message = 'The value you selected is not a valid choice.'; From fdb79728b601fc3e568d5351d41d449507668270 Mon Sep 17 00:00:00 2001 From: Peter Rehm Date: Sun, 10 Jul 2016 11:39:18 +0200 Subject: [PATCH 2/5] Deprecated setting the strict option to false in the choice constraint --- .../Validator/Constraints/Choice.php | 2 +- .../Validator/Constraints/ChoiceValidator.php | 4 ++ .../Tests/Constraints/ChoiceValidatorTest.php | 40 ++++++++++++++----- 3 files changed, 35 insertions(+), 11 deletions(-) diff --git a/src/Symfony/Component/Validator/Constraints/Choice.php b/src/Symfony/Component/Validator/Constraints/Choice.php index 8c4c23ad0ed65..4b93c70e4a5f4 100644 --- a/src/Symfony/Component/Validator/Constraints/Choice.php +++ b/src/Symfony/Component/Validator/Constraints/Choice.php @@ -34,7 +34,7 @@ class Choice extends Constraint public $choices; public $callback; public $multiple = false; - public $strict = true; + public $strict = false; public $min; public $max; public $message = 'The value you selected is not a valid choice.'; diff --git a/src/Symfony/Component/Validator/Constraints/ChoiceValidator.php b/src/Symfony/Component/Validator/Constraints/ChoiceValidator.php index 58e4e9142662d..1d2d86550d784 100644 --- a/src/Symfony/Component/Validator/Constraints/ChoiceValidator.php +++ b/src/Symfony/Component/Validator/Constraints/ChoiceValidator.php @@ -57,6 +57,10 @@ public function validate($value, Constraint $constraint) $choices = $constraint->choices; } + if (false === $constraint->strict) { + @trigger_error('Setting the strict option of the Choice constraint to false is deprecated since version 3.2 and will be removed in 4.0.', E_USER_DEPRECATED); + } + if ($constraint->multiple) { foreach ($value as $_value) { if (!in_array($_value, $choices, $constraint->strict)) { diff --git a/src/Symfony/Component/Validator/Tests/Constraints/ChoiceValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/ChoiceValidatorTest.php index 0e1b7066ad352..01bb124dae4fd 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/ChoiceValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/ChoiceValidatorTest.php @@ -40,6 +40,7 @@ public function testExpectArrayIfMultipleIsTrue() $constraint = new Choice(array( 'choices' => array('foo', 'bar'), 'multiple' => true, + 'strict' => true, )); $this->validator->validate('asdf', $constraint); @@ -47,7 +48,15 @@ public function testExpectArrayIfMultipleIsTrue() public function testNullIsValid() { - $this->validator->validate(null, new Choice(array('choices' => array('foo', 'bar')))); + $this->validator->validate( + null, + new Choice( + array( + 'choices' => array('foo', 'bar'), + 'strict' => true, + ) + ) + ); $this->assertNoViolation(); } @@ -57,7 +66,7 @@ public function testNullIsValid() */ public function testChoicesOrCallbackExpected() { - $this->validator->validate('foobar', new Choice()); + $this->validator->validate('foobar', new Choice(array('strict' => true))); } /** @@ -65,12 +74,12 @@ public function testChoicesOrCallbackExpected() */ public function testValidCallbackExpected() { - $this->validator->validate('foobar', new Choice(array('callback' => 'abcd'))); + $this->validator->validate('foobar', new Choice(array('callback' => 'abcd', 'strict' => true))); } public function testValidChoiceArray() { - $constraint = new Choice(array('choices' => array('foo', 'bar'))); + $constraint = new Choice(array('choices' => array('foo', 'bar'), 'strict' => true)); $this->validator->validate('bar', $constraint); @@ -79,7 +88,7 @@ public function testValidChoiceArray() public function testValidChoiceCallbackFunction() { - $constraint = new Choice(array('callback' => __NAMESPACE__.'\choice_callback')); + $constraint = new Choice(array('callback' => __NAMESPACE__.'\choice_callback', 'strict' => true)); $this->validator->validate('bar', $constraint); @@ -88,9 +97,14 @@ public function testValidChoiceCallbackFunction() public function testValidChoiceCallbackClosure() { - $constraint = new Choice(array('callback' => function () { - return array('foo', 'bar'); - })); + $constraint = new Choice( + array( + 'strict' => true, + 'callback' => function () { + return array('foo', 'bar'); + }, + ) + ); $this->validator->validate('bar', $constraint); @@ -99,7 +113,7 @@ public function testValidChoiceCallbackClosure() public function testValidChoiceCallbackStaticMethod() { - $constraint = new Choice(array('callback' => array(__CLASS__, 'staticCallback'))); + $constraint = new Choice(array('callback' => array(__CLASS__, 'staticCallback'), 'strict' => true)); $this->validator->validate('bar', $constraint); @@ -111,7 +125,7 @@ public function testValidChoiceCallbackContextMethod() // search $this for "staticCallback" $this->setObject($this); - $constraint = new Choice(array('callback' => 'staticCallback')); + $constraint = new Choice(array('callback' => 'staticCallback', 'strict' => true)); $this->validator->validate('bar', $constraint); @@ -123,6 +137,7 @@ public function testMultipleChoices() $constraint = new Choice(array( 'choices' => array('foo', 'bar', 'baz'), 'multiple' => true, + 'strict' => true, )); $this->validator->validate(array('baz', 'bar'), $constraint); @@ -135,6 +150,7 @@ public function testInvalidChoice() $constraint = new Choice(array( 'choices' => array('foo', 'bar'), 'message' => 'myMessage', + 'strict' => true, )); $this->validator->validate('baz', $constraint); @@ -152,6 +168,7 @@ public function testInvalidChoiceEmptyChoices() // the DB or the model 'choices' => array(), 'message' => 'myMessage', + 'strict' => true, )); $this->validator->validate('baz', $constraint); @@ -168,6 +185,7 @@ public function testInvalidChoiceMultiple() 'choices' => array('foo', 'bar'), 'multipleMessage' => 'myMessage', 'multiple' => true, + 'strict' => true, )); $this->validator->validate(array('foo', 'baz'), $constraint); @@ -186,6 +204,7 @@ public function testTooFewChoices() 'multiple' => true, 'min' => 2, 'minMessage' => 'myMessage', + 'strict' => true, )); $value = array('foo'); @@ -209,6 +228,7 @@ public function testTooManyChoices() 'multiple' => true, 'max' => 2, 'maxMessage' => 'myMessage', + 'strict' => true, )); $value = array('foo', 'bar', 'moo'); From 3c7486ebe211f76e6fa7bed22bd88b969cd8642c Mon Sep 17 00:00:00 2001 From: Peter Rehm Date: Wed, 13 Jul 2016 07:32:06 +0200 Subject: [PATCH 3/5] Marked non strict tests as legacy --- .../Validator/Tests/Constraints/ChoiceValidatorTest.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/Symfony/Component/Validator/Tests/Constraints/ChoiceValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/ChoiceValidatorTest.php index 01bb124dae4fd..203da55c5f074 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/ChoiceValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/ChoiceValidatorTest.php @@ -245,6 +245,9 @@ public function testTooManyChoices() ->assertRaised(); } + /** + * @group legacy + */ public function testNonStrict() { $constraint = new Choice(array( @@ -286,6 +289,9 @@ public function testStrictDisallowsDifferentType() ->assertRaised(); } + /** + * @group legacy + */ public function testNonStrictWithMultipleChoices() { $constraint = new Choice(array( From 5c3b69aaa9087750611ebf33cdc4db6badd7a6a2 Mon Sep 17 00:00:00 2001 From: Peter Rehm Date: Wed, 13 Jul 2016 07:37:29 +0200 Subject: [PATCH 4/5] Updated upgrade information --- UPGRADE-3.2.md | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/UPGRADE-3.2.md b/UPGRADE-3.2.md index edd6d70cb5e70..d2cd8e92bcece 100644 --- a/UPGRADE-3.2.md +++ b/UPGRADE-3.2.md @@ -70,3 +70,19 @@ Validator // ... } ``` + + * Setting the strict option of the `Choice` Constraint to `false` has been + deprecated and the option will be changed to `true` as of 4.0. + + ```php + // ... + use Symfony\Component\Validator\Constraints as Assert; + + class MyEntity + { + /** + * @Assert\Choice(choices={"MR", "MRS"}, strict=true) + */ + private $salutation; + } + ``` From f4f62b94ed3eca3c426e392e5f791d6d0ad5c9db Mon Sep 17 00:00:00 2001 From: Peter Rehm Date: Mon, 8 Aug 2016 11:43:37 +0200 Subject: [PATCH 5/5] Added upgrade information for 4.0 --- UPGRADE-4.0.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/UPGRADE-4.0.md b/UPGRADE-4.0.md index 2abe339922a9c..62dc9ab48ceda 100644 --- a/UPGRADE-4.0.md +++ b/UPGRADE-4.0.md @@ -261,3 +261,7 @@ Validator // ... } ``` + + * The default value of the strict option of the `Choice` Constraint has been + changed to `true` as of 4.0. If you need the the previous behaviour ensure to + set the option to `false`.