[Form] Fixed FormFactory not to set "data" option if not explicitely given
This commit is contained in:
parent
7149d268b6
commit
2bf4d6cff4
@ -149,7 +149,7 @@ class FormType extends AbstractType
|
|||||||
{
|
{
|
||||||
// Derive "data_class" option from passed "data" object
|
// Derive "data_class" option from passed "data" object
|
||||||
$dataClass = function (Options $options) {
|
$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
|
// Derive "empty_data" closure from "data_class" option
|
||||||
|
@ -145,7 +145,7 @@ class FormFactory implements FormFactoryInterface
|
|||||||
*/
|
*/
|
||||||
public function createNamedBuilder($name, $type, $data = null, array $options = array(), FormBuilderInterface $parent = null)
|
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;
|
$options['data'] = $data;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -16,13 +16,19 @@ use Symfony\Component\Form\FormBuilder;
|
|||||||
use Symfony\Component\Form\FormBuilderInterface;
|
use Symfony\Component\Form\FormBuilderInterface;
|
||||||
use Symfony\Component\Form\FormFactoryInterface;
|
use Symfony\Component\Form\FormFactoryInterface;
|
||||||
use Symfony\Component\EventDispatcher\EventDispatcher;
|
use Symfony\Component\EventDispatcher\EventDispatcher;
|
||||||
|
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
|
||||||
|
|
||||||
class FooType extends AbstractType
|
class FooType extends AbstractType
|
||||||
{
|
{
|
||||||
public function buildForm(FormBuilderInterface $builder, array $options)
|
public function buildForm(FormBuilderInterface $builder, array $options)
|
||||||
{
|
{
|
||||||
$builder->setAttribute('foo', 'x');
|
$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()
|
public function getName()
|
||||||
@ -35,21 +41,21 @@ class FooType extends AbstractType
|
|||||||
return new FormBuilder($name, null, new EventDispatcher(), $factory);
|
return new FormBuilder($name, null, new EventDispatcher(), $factory);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getDefaultOptions()
|
public function setDefaultOptions(OptionsResolverInterface $resolver)
|
||||||
{
|
{
|
||||||
return array(
|
$resolver->setDefaults(array(
|
||||||
'data' => null,
|
|
||||||
'required' => false,
|
'required' => false,
|
||||||
'max_length' => null,
|
'max_length' => null,
|
||||||
'a_or_b' => 'a',
|
'a_or_b' => 'a',
|
||||||
);
|
));
|
||||||
}
|
|
||||||
|
|
||||||
public function getAllowedOptionValues()
|
$resolver->setOptional(array(
|
||||||
{
|
'data',
|
||||||
return array(
|
));
|
||||||
|
|
||||||
|
$resolver->setAllowedValues(array(
|
||||||
'a_or_b' => array('a', 'b'),
|
'a_or_b' => array('a', 'b'),
|
||||||
);
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getParent()
|
public function getParent()
|
||||||
|
@ -150,9 +150,21 @@ class FormFactoryTest extends \PHPUnit_Framework_TestCase
|
|||||||
$builder = $this->factory->createNamedBuilder('bar', 'foo', 'xyz');
|
$builder = $this->factory->createNamedBuilder('bar', 'foo', 'xyz');
|
||||||
|
|
||||||
// see FooType::buildForm()
|
// see FooType::buildForm()
|
||||||
|
$this->assertTrue($builder->getAttribute('data_option_set'));
|
||||||
$this->assertEquals('xyz', $builder->getAttribute('data_option'));
|
$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()
|
public function testCreateNamedBuilderDoesNotOverrideExistingDataOption()
|
||||||
{
|
{
|
||||||
$type = new FooType();
|
$type = new FooType();
|
||||||
@ -163,6 +175,7 @@ class FormFactoryTest extends \PHPUnit_Framework_TestCase
|
|||||||
));
|
));
|
||||||
|
|
||||||
// see FooType::buildForm()
|
// see FooType::buildForm()
|
||||||
|
$this->assertTrue($builder->getAttribute('data_option_set'));
|
||||||
$this->assertEquals('abc', $builder->getAttribute('data_option'));
|
$this->assertEquals('abc', $builder->getAttribute('data_option'));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user