Skip to content

Commit 50c7c3d

Browse files
committed
bug #39002 [Validator] Override the default option of the choice constraint (benji07)
This PR was merged into the 5.2-dev branch. Discussion ---------- [Validator] Override the default option of the choice constraint | Q | A | ------------- | --- | Branch? | 5.2 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | N/A | License | MIT | Doc PR | TODO We have a bundle that run test against the last version of symfony and we detect BC Break when passing a string as the first argument of the class Choice Our code extends the Choice class and change the defaultOption. I saw that others constraints class had new construct signature (for php8 attributes), but only some of them kept the array options as their first arguments. Why ? ping @ogizanagi @derrabus https://travis-ci.com/github/Elao/PhpEnums/jobs/410045368 ``` 1) Elao\Enum\Tests\Unit\Bridge\Symfony\Validator\Constraint\EnumTest::testDefaultValueIsEnumClass Symfony\Component\Validator\Exception\MissingOptionsException: The options "class" must be set for constraint "Elao\Enum\Bridge\Symfony\Validator\Constraint\Enum". /home/travis/build/Elao/PhpEnums/vendor/symfony/symfony/src/Symfony/Component/Validator/Constraint.php:171 /home/travis/build/Elao/PhpEnums/vendor/symfony/symfony/src/Symfony/Component/Validator/Constraint.php:110 /home/travis/build/Elao/PhpEnums/vendor/symfony/symfony/src/Symfony/Component/Validator/Constraints/Choice.php:75 /home/travis/build/Elao/PhpEnums/src/Bridge/Symfony/Validator/Constraint/Enum.php:39 /home/travis/build/Elao/PhpEnums/tests/Unit/Bridge/Symfony/Validator/Constraint/EnumTest.php:22 ``` Commits ------- d553750 Allow user to override default options when extending the Choice Constraint
2 parents f226d98 + d553750 commit 50c7c3d

File tree

3 files changed

+60
-1
lines changed

3 files changed

+60
-1
lines changed

src/Symfony/Component/Validator/Constraints/Choice.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public function __construct(
6969
if (\is_array($choices) && \is_string(key($choices))) {
7070
$options = array_merge($choices, $options);
7171
} elseif (null !== $choices) {
72-
$options['choices'] = $choices;
72+
$options['value'] = $choices;
7373
}
7474

7575
parent::__construct($options, $groups, $payload);
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Validator\Tests\Constraints;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\Validator\Tests\Fixtures\ConstraintChoiceWithPreset;
16+
17+
class ChoiceTest extends TestCase
18+
{
19+
public function testSetDefaultPropertyChoice()
20+
{
21+
$constraint = new ConstraintChoiceWithPreset('A');
22+
23+
self::assertEquals(['A', 'B', 'C'], $constraint->choices);
24+
}
25+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Validator\Tests\Fixtures;
13+
14+
use Symfony\Component\Validator\Constraints\Choice;
15+
16+
class ConstraintChoiceWithPreset extends Choice
17+
{
18+
public $type;
19+
20+
public function __construct(string $type) {
21+
parent::__construct($type);
22+
23+
if ($this->type === 'A') {
24+
$this->choices = ['A', 'B', 'C'];
25+
} else {
26+
$this->choices = ['D', 'E', 'F'];
27+
}
28+
}
29+
30+
public function getDefaultOption(): ?string
31+
{
32+
return 'type';
33+
}
34+
}

0 commit comments

Comments
 (0)