Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[Form] Add option to ignore invalid choices#10008

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Closed
trsteel88 wants to merge3 commits intosymfony:masterfromtrsteel88:master
Closed
Show file tree
Hide file tree
Changes fromall 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
Original file line numberDiff line numberDiff line change
Expand Up@@ -23,14 +23,18 @@ class ChoicesToValuesTransformer implements DataTransformerInterface
{
private $choiceList;

private $ignoreInvalidChoices;

/**
* Constructor.
*
* @param ChoiceListInterface $choiceList
* @param bool $ignoreInvalidChoices
*/
public function __construct(ChoiceListInterface $choiceList)
public function __construct(ChoiceListInterface $choiceList, $ignoreInvalidChoices)
{
$this->choiceList = $choiceList;
$this->ignoreInvalidChoices = $ignoreInvalidChoices;
}

/**
Expand DownExpand Up@@ -74,7 +78,7 @@ public function reverseTransform($array)

$choices = $this->choiceList->getChoicesForValues($array);

if (count($choices) !== count($array)) {
if (!$this->ignoreInvalidChoices &&count($choices) !== count($array)) {
throw new TransformationFailedException('Could not find all matching choices for the given values');
}

Expand Down
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -27,14 +27,18 @@ class FixCheckboxInputListener implements EventSubscriberInterface
{
private $choiceList;

private $ignoreInvalidChoices;

/**
* Constructor.
*
* @param ChoiceListInterface $choiceList
* @param bool $ignoreInvalidChoices
*/
public function __construct(ChoiceListInterface $choiceList)
public function __construct(ChoiceListInterface $choiceList, $ignoreInvalidChoices)
{
$this->choiceList = $choiceList;
$this->ignoreInvalidChoices = $ignoreInvalidChoices;
}

public function preSubmit(FormEvent $event)
Expand DownExpand Up@@ -62,7 +66,7 @@ public function preSubmit(FormEvent $event)
}
}

if (count($submittedValues) > 0) {
if (!$this->ignoreInvalidChoices &&count($submittedValues) > 0) {
throw new TransformationFailedException(sprintf(
'The following choices were not found: "%s"',
implode('", "', array_keys($submittedValues))
Expand Down
25 changes: 13 additions & 12 deletionssrc/Symfony/Component/Form/Extension/Core/Type/ChoiceType.php
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -70,14 +70,14 @@ public function buildForm(FormBuilderInterface $builder, array $options)

if ($options['multiple']) {
$builder->addViewTransformer(new ChoicesToBooleanArrayTransformer($options['choice_list']));
$builder->addEventSubscriber(new FixCheckboxInputListener($options['choice_list']), 10);
$builder->addEventSubscriber(new FixCheckboxInputListener($options['choice_list'], $options['ignore_invalid_choices']), 10);
} else {
$builder->addViewTransformer(new ChoiceToBooleanArrayTransformer($options['choice_list'], $builder->has('placeholder')));
$builder->addEventSubscriber(new FixRadioInputListener($options['choice_list'], $builder->has('placeholder')), 10);
}
} else {
if ($options['multiple']) {
$builder->addViewTransformer(new ChoicesToValuesTransformer($options['choice_list']));
$builder->addViewTransformer(new ChoicesToValuesTransformer($options['choice_list'], $options['ignore_invalid_choices']));
} else {
$builder->addViewTransformer(new ChoiceToValueTransformer($options['choice_list']));
}
Expand DownExpand Up@@ -208,19 +208,20 @@ public function setDefaultOptions(OptionsResolverInterface $resolver)
};

$resolver->setDefaults(array(
'multiple' => false,
'expanded' => false,
'choice_list' => $choiceList,
'choices' => array(),
'preferred_choices' => array(),
'empty_data' => $emptyData,
'empty_value' => $emptyValue,
'error_bubbling' => false,
'compound' => $compound,
'multiple' => false,
'expanded' => false,
'choice_list' => $choiceList,
'choices' => array(),
'preferred_choices' => array(),
'ignore_invalid_choices' => false,
'empty_data' => $emptyData,
'empty_value' => $emptyValue,
'error_bubbling' => false,
'compound' => $compound,
// The view data is always a string, even if the "data" option
// is manually set to an object.
// See https://github.com/symfony/symfony/pull/5582
'data_class' => null,
'data_class'=> null,
));

$resolver->setNormalizers(array(
Expand Down
Original file line numberDiff line numberDiff line change
Expand Up@@ -19,15 +19,19 @@ class ChoicesToValuesTransformerTest extends \PHPUnit_Framework_TestCase
{
protected $transformer;

protected $transformer2;

protected function setUp()
{
$list = new SimpleChoiceList(array(0 => 'A', 1 => 'B', 2 => 'C'));
$this->transformer = new ChoicesToValuesTransformer($list);
$this->transformer = new ChoicesToValuesTransformer($list, false);
$this->transformer2 = new ChoicesToValuesTransformer($list, true);
}

protected function tearDown()
{
$this->transformer = null;
$this->transformer2 = null;
}

public function testTransform()
Expand DownExpand Up@@ -73,4 +77,13 @@ public function testReverseTransformExpectsArray()
{
$this->transformer->reverseTransform('foobar');
}

public function testIgnoreInvalidChoicesTransform()
{
// Value strategy in SimpleChoiceList is to copy and convert to string
$in = array(2, 3, 4, 5);
$out = array('2');

$this->assertSame($out, $this->transformer2->transform($in));
}
}

[8]ページ先頭

©2009-2025 Movatter.jp