diff --git a/src/Symfony/Component/Validator/Constraints/Choice.php b/src/Symfony/Component/Validator/Constraints/Choice.php index 0d7a2b7deb..86d199f867 100644 --- a/src/Symfony/Component/Validator/Constraints/Choice.php +++ b/src/Symfony/Component/Validator/Constraints/Choice.php @@ -69,7 +69,7 @@ class Choice extends Constraint if (\is_array($choices) && \is_string(key($choices))) { $options = array_merge($choices, $options); } elseif (null !== $choices) { - $options['choices'] = $choices; + $options['value'] = $choices; } parent::__construct($options, $groups, $payload); diff --git a/src/Symfony/Component/Validator/Tests/Constraints/ChoiceTest.php b/src/Symfony/Component/Validator/Tests/Constraints/ChoiceTest.php new file mode 100644 index 0000000000..8b5879aedc --- /dev/null +++ b/src/Symfony/Component/Validator/Tests/Constraints/ChoiceTest.php @@ -0,0 +1,25 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Validator\Tests\Constraints; + +use PHPUnit\Framework\TestCase; +use Symfony\Component\Validator\Tests\Fixtures\ConstraintChoiceWithPreset; + +class ChoiceTest extends TestCase +{ + public function testSetDefaultPropertyChoice() + { + $constraint = new ConstraintChoiceWithPreset('A'); + + self::assertEquals(['A', 'B', 'C'], $constraint->choices); + } +} diff --git a/src/Symfony/Component/Validator/Tests/Fixtures/ConstraintChoiceWithPreset.php b/src/Symfony/Component/Validator/Tests/Fixtures/ConstraintChoiceWithPreset.php new file mode 100644 index 0000000000..9e0cc37e23 --- /dev/null +++ b/src/Symfony/Component/Validator/Tests/Fixtures/ConstraintChoiceWithPreset.php @@ -0,0 +1,34 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Validator\Tests\Fixtures; + +use Symfony\Component\Validator\Constraints\Choice; + +class ConstraintChoiceWithPreset extends Choice +{ + public $type; + + public function __construct(string $type) { + parent::__construct($type); + + if ($this->type === 'A') { + $this->choices = ['A', 'B', 'C']; + } else { + $this->choices = ['D', 'E', 'F']; + } + } + + public function getDefaultOption(): ?string + { + return 'type'; + } +}