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

Appearance settings

[Form] Removed useless code #16725

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 28, 2015
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
36 changes: 6 additions & 30 deletions 36 src/Symfony/Bridge/Doctrine/Tests/Form/Type/EntityTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -982,37 +982,13 @@ public function testLoaderCaching()
'property3' => 2,
));

$choiceList1 = $form->get('property1')->getConfig()->getOption('choice_list');
$choiceList2 = $form->get('property2')->getConfig()->getOption('choice_list');
$choiceList3 = $form->get('property3')->getConfig()->getOption('choice_list');
$choiceLoader1 = $form->get('property1')->getConfig()->getOption('choice_loader');
$choiceLoader2 = $form->get('property2')->getConfig()->getOption('choice_loader');
$choiceLoader3 = $form->get('property3')->getConfig()->getOption('choice_loader');

$this->assertInstanceOf('Symfony\Component\Form\ChoiceList\ChoiceListInterface', $choiceList1);
$this->assertSame($choiceList1, $choiceList2);
$this->assertSame($choiceList1, $choiceList3);
}

public function testCacheChoiceLists()
{
$entity1 = new SingleIntIdEntity(1, 'Foo');

$this->persist(array($entity1));

$field1 = $this->factory->createNamed('name', 'Symfony\Bridge\Doctrine\Form\Type\EntityType', null, array(
'em' => 'default',
'class' => self::SINGLE_IDENT_CLASS,
'required' => false,
'choice_label' => 'name',
));

$field2 = $this->factory->createNamed('name', 'Symfony\Bridge\Doctrine\Form\Type\EntityType', null, array(
'em' => 'default',
'class' => self::SINGLE_IDENT_CLASS,
'required' => false,
'choice_label' => 'name',
));

$this->assertInstanceOf('Symfony\Component\Form\ChoiceList\ChoiceListInterface', $field1->getConfig()->getOption('choice_list'));
$this->assertSame($field1->getConfig()->getOption('choice_list'), $field2->getConfig()->getOption('choice_list'));
$this->assertInstanceOf('Symfony\Component\Form\ChoiceList\Loader\ChoiceLoaderInterface', $choiceLoader1);
$this->assertSame($choiceLoader1, $choiceLoader2);
$this->assertSame($choiceLoader1, $choiceLoader3);
}

protected function createRegistryMock($name, $em)
Expand Down
73 changes: 40 additions & 33 deletions 73 src/Symfony/Component/Form/Extension/Core/Type/ChoiceType.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,13 @@ public function __construct(ChoiceListFactoryInterface $choiceListFactory = null
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$choiceList = $this->createChoiceList($options);
$builder->setAttribute('choice_list', $choiceList);

if ($options['expanded']) {
$builder->setDataMapper($options['multiple']
? new CheckboxListMapper($options['choice_list'])
: new RadioListMapper($options['choice_list']));
? new CheckboxListMapper($choiceList)
: new RadioListMapper($choiceList));

// Initialize all choices before doing the index check below.
// This helps in cases where index checks are optimized for non
Expand All @@ -64,12 +67,12 @@ public function buildForm(FormBuilderInterface $builder, array $options)
// requires another SQL query. When the initialization is done first,
// one SQL query is sufficient.

$choiceListView = $this->createChoiceListView($options['choice_list'], $options);
$choiceListView = $this->createChoiceListView($choiceList, $options);
$builder->setAttribute('choice_list_view', $choiceListView);

// Check if the choices already contain the empty value
// Only add the placeholder option if this is not the case
if (null !== $options['placeholder'] && 0 === count($options['choice_list']->getChoicesForValues(array('')))) {
if (null !== $options['placeholder'] && 0 === count($choiceList->getChoicesForValues(array('')))) {
$placeholderView = new ChoiceView(null, '', $options['placeholder']);

// "placeholder" is a reserved name
Expand Down Expand Up @@ -139,10 +142,10 @@ public function buildForm(FormBuilderInterface $builder, array $options)
}
} elseif ($options['multiple']) {
// <select> tag with "multiple" option
$builder->addViewTransformer(new ChoicesToValuesTransformer($options['choice_list']));
$builder->addViewTransformer(new ChoicesToValuesTransformer($choiceList));
} else {
// <select> tag without "multiple" option
$builder->addViewTransformer(new ChoiceToValueTransformer($options['choice_list']));
$builder->addViewTransformer(new ChoiceToValueTransformer($choiceList));
}

if ($options['multiple'] && $options['by_reference']) {
Expand All @@ -162,10 +165,13 @@ public function buildView(FormView $view, FormInterface $form, array $options)
$choiceTranslationDomain = $view->vars['translation_domain'];
}

/** @var ChoiceListInterface $choiceList */
$choiceList = $form->getConfig()->getAttribute('choice_list');

/** @var ChoiceListView $choiceListView */
$choiceListView = $form->getConfig()->hasAttribute('choice_list_view')
? $form->getConfig()->getAttribute('choice_list_view')
: $this->createChoiceListView($options['choice_list'], $options);
: $this->createChoiceListView($choiceList, $options);

$view->vars = array_replace($view->vars, array(
'multiple' => $options['multiple'],
Expand All @@ -192,7 +198,7 @@ public function buildView(FormView $view, FormInterface $form, array $options)
}

// Check if the choices already contain the empty value
$view->vars['placeholder_in_choices'] = 0 !== count($options['choice_list']->getChoicesForValues(array('')));
$view->vars['placeholder_in_choices'] = 0 !== count($choiceList->getChoicesForValues(array('')));

// Only add the empty value option if this is not the case
if (null !== $options['placeholder'] && !$view->vars['placeholder_in_choices']) {
Expand Down Expand Up @@ -235,8 +241,6 @@ public function finishView(FormView $view, FormInterface $form, array $options)
*/
public function configureOptions(OptionsResolver $resolver)
{
$choiceListFactory = $this->choiceListFactory;

$emptyData = function (Options $options) {
if ($options['multiple'] || $options['expanded']) {
return array();
Expand All @@ -249,29 +253,20 @@ public function configureOptions(OptionsResolver $resolver)
return $options['required'] ? null : '';
};

$choiceListNormalizer = function (Options $options) use ($choiceListFactory) {
if (null !== $options['choice_loader']) {
return $choiceListFactory->createListFromLoader(
$options['choice_loader'],
$options['choice_value']
);
$choicesAsValuesNormalizer = function (Options $options, $choicesAsValues) {
// Not set by the user
if (null === $choicesAsValues) {
return true;
}

// Harden against NULL values (like in EntityType and ModelType)
$choices = null !== $options['choices'] ? $options['choices'] : array();

return $choiceListFactory->createListFromChoices($choices, $options['choice_value']);
};

$choicesAsValuesNormalizer = function (Options $options, $choicesAsValues) {
if (null !== $choicesAsValues) {
if (true !== $choicesAsValues) {
throw new \RuntimeException('The "choices_as_values" option should not be used. Remove it and flip the contents of the "choices" option instead.');
}
// To be uncommented in 3.1
//@trigger_error('The "choices_as_values" option is deprecated since version 3.1 and will be removed in 4.0. You should not use it anymore.', E_USER_DEPRECATED);
// Set by the user
if (true !== $choicesAsValues) {
throw new \RuntimeException('The "choices_as_values" option should not be used. Remove it and flip the contents of the "choices" option instead.');
}

// To be uncommented in 3.1
//@trigger_error('The "choices_as_values" option is deprecated since version 3.1 and will be removed in 4.0. You should not use it anymore.', E_USER_DEPRECATED);

return true;
};

Expand Down Expand Up @@ -306,9 +301,8 @@ public function configureOptions(OptionsResolver $resolver)
$resolver->setDefaults(array(
'multiple' => false,
'expanded' => false,
'choice_list' => null, // deprecated
'choices' => array(),
'choices_as_values' => null, // to be deprecated in 3.1
'choices_as_values' => null, // to be deprecated in 3.1
'choice_loader' => null,
'choice_label' => null,
'choice_name' => null,
Expand All @@ -327,12 +321,10 @@ public function configureOptions(OptionsResolver $resolver)
'choice_translation_domain' => true,
));

$resolver->setNormalizer('choice_list', $choiceListNormalizer);
$resolver->setNormalizer('placeholder', $placeholderNormalizer);
$resolver->setNormalizer('choice_translation_domain', $choiceTranslationDomainNormalizer);
$resolver->setNormalizer('choices_as_values', $choicesAsValuesNormalizer);

$resolver->setAllowedTypes('choice_list', array('null', 'Symfony\Component\Form\ChoiceList\ChoiceListInterface'));
$resolver->setAllowedTypes('choices', array('null', 'array', '\Traversable'));
$resolver->setAllowedTypes('choice_translation_domain', array('null', 'bool', 'string'));
$resolver->setAllowedTypes('choice_loader', array('null', 'Symfony\Component\Form\ChoiceList\Loader\ChoiceLoaderInterface'));
Expand Down Expand Up @@ -422,6 +414,21 @@ private function addSubForm(FormBuilderInterface $builder, $name, ChoiceView $ch
$builder->add($name, $choiceType, $choiceOpts);
}

private function createChoiceList(array $options)
{
if (null !== $options['choice_loader']) {
return $this->choiceListFactory->createListFromLoader(
$options['choice_loader'],
$options['choice_value']
);
}

// Harden against NULL values (like in EntityType and ModelType)
$choices = null !== $options['choices'] ? $options['choices'] : array();

return $this->choiceListFactory->createListFromChoices($choices, $options['choice_value']);
}

private function createChoiceListView(ChoiceListInterface $choiceList, array $options)
{
// If no explicit grouping information is given, use the structural
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,16 +68,6 @@ public function testChoicesOptionExpectsArrayOrTraversable()
));
}

/**
* @expectedException \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException
*/
public function testChoiceListOptionExpectsChoiceListInterface()
{
$this->factory->create('Symfony\Component\Form\Extension\Core\Type\ChoiceType', null, array(
'choice_list' => array('foo' => 'foo'),
));
}

/**
* @expectedException \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException
*/
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.