[Form] Fixed FormFactory not to set "data" option if not explicitely given

This commit is contained in:
Bernhard Schussek 2012-07-13 12:12:25 +02:00
parent 7149d268b6
commit 2bf4d6cff4
4 changed files with 31 additions and 12 deletions

View File

@ -149,7 +149,7 @@ class FormType extends AbstractType
{
// Derive "data_class" option from passed "data" object
$dataClass = function (Options $options) {
return is_object($options['data']) ? get_class($options['data']) : null;
return isset($options['data']) && is_object($options['data']) ? get_class($options['data']) : null;
};
// Derive "empty_data" closure from "data_class" option

View File

@ -145,7 +145,7 @@ class FormFactory implements FormFactoryInterface
*/
public function createNamedBuilder($name, $type, $data = null, array $options = array(), FormBuilderInterface $parent = null)
{
if (!array_key_exists('data', $options)) {
if (null !== $data && !array_key_exists('data', $options)) {
$options['data'] = $data;
}

View File

@ -16,13 +16,19 @@ use Symfony\Component\Form\FormBuilder;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormFactoryInterface;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
class FooType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->setAttribute('foo', 'x');
$builder->setAttribute('data_option', $options['data']);
$builder->setAttribute('data_option', isset($options['data']) ? $options['data'] : null);
// Important: array_key_exists(), not isset()
// -> The "data" option is optional in FormType
// If it is given, the form's data will be locked to the value of the option
// Thus "data" must not be set in the array unless explicitely specified
$builder->setAttribute('data_option_set', array_key_exists('data', $options));
}
public function getName()
@ -35,21 +41,21 @@ class FooType extends AbstractType
return new FormBuilder($name, null, new EventDispatcher(), $factory);
}
public function getDefaultOptions()
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
return array(
'data' => null,
$resolver->setDefaults(array(
'required' => false,
'max_length' => null,
'a_or_b' => 'a',
);
}
));
public function getAllowedOptionValues()
{
return array(
$resolver->setOptional(array(
'data',
));
$resolver->setAllowedValues(array(
'a_or_b' => array('a', 'b'),
);
));
}
public function getParent()

View File

@ -150,9 +150,21 @@ class FormFactoryTest extends \PHPUnit_Framework_TestCase
$builder = $this->factory->createNamedBuilder('bar', 'foo', 'xyz');
// see FooType::buildForm()
$this->assertTrue($builder->getAttribute('data_option_set'));
$this->assertEquals('xyz', $builder->getAttribute('data_option'));
}
public function testCreateNamedBuilderDoesNotSetDataOptionIfNull()
{
$type = new FooType();
$this->extension1->addType($type);
$builder = $this->factory->createNamedBuilder('bar', 'foo', null);
// see FooType::buildForm()
$this->assertFalse($builder->getAttribute('data_option_set'));
}
public function testCreateNamedBuilderDoesNotOverrideExistingDataOption()
{
$type = new FooType();
@ -163,6 +175,7 @@ class FormFactoryTest extends \PHPUnit_Framework_TestCase
));
// see FooType::buildForm()
$this->assertTrue($builder->getAttribute('data_option_set'));
$this->assertEquals('abc', $builder->getAttribute('data_option'));
}