8000 [Validator] Override the default option of the choice constraint by benji07 · Pull Request #39002 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Validator] Override the default option of the choice constraint #39002

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Allow user to override default options when extending the Choice Cons…
…traint

Co-authored-by: Maxime Steinhausser <maxime.steinhausser@gmail.com>
  • Loading branch information
benji07 and ogizanagi committed Nov 7, 2020
commit d55375005403062e6fe3c9acc0b2d9aaacb26cf3
2 changes: 1 addition & 1 deletion src/Symfony/Component/Validator/Constraints/Choice.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public function __construct(
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);
Expand Down
25 changes: 25 additions & 0 deletions src/Symfony/Component/Validator/Tests/Constraints/ChoiceTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* 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);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* 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';
}
}
0