Skip to content

Commit

Permalink
[Form] Fixed setting the "data" option to an object in "choice" and "…
Browse files Browse the repository at this point in the history
…entity" type
  • Loading branch information
webmozart committed Oct 18, 2012
1 parent defccb3 commit 8f81f07
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/Symfony/Component/Form/Extension/Core/Type/ChoiceType.php
Expand Up @@ -198,6 +198,10 @@ public function setDefaultOptions(OptionsResolverInterface $resolver)
'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,
));

$resolver->setNormalizers(array(
Expand Down
Expand Up @@ -745,4 +745,22 @@ public function testInitializeWithEmptyChoices()
'choices' => array(),
));
}

public function testInitializeWithDefaultObjectChoice()
{
$obj1 = (object) array('value' => 'a', 'label' => 'A');
$obj2 = (object) array('value' => 'b', 'label' => 'B');
$obj3 = (object) array('value' => 'c', 'label' => 'C');
$obj4 = (object) array('value' => 'd', 'label' => 'D');

$form = $this->factory->create('choice', null, array(
'choice_list' => new ObjectChoiceList(array($obj1, $obj2, $obj3, $obj4), 'label', array(), null, 'value'),
// Used to break because "data_class" was inferred, which needs to
// remain null in every case (because it refers to the view format)
'data' => $obj3,
));

// Trigger data initialization
$form->getViewData();
}
}

0 comments on commit 8f81f07

Please sign in to comment.