8000 [Form] Added an alternative signature Form::add($name, $type, $options) · symfony/symfony@fb71964 · GitHub
[go: up one dir, main page]

Skip to content

Commit fb71964

Browse files
committed
[Form] Added an alternative signature Form::add($name, $type, $options)
1 parent d9f7844 commit fb71964

File tree

15 files changed

+184
-71
lines changed

15 files changed

+184
-71
lines changed

UPGRADE-2.2.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,14 @@
3737

3838
### Form
3939

40-
* The PasswordType is now not trimmed by default.
40+
* The PasswordType is now not trimmed by default.
41+
42+
#### Deprecations
43+
44+
* The methods `getParent()`, `setParent()` and `hasParent()` in
45+
`FormBuilderInterface` were deprecated and will be removed in Symfony 2.3.
46+
You should not rely on these methods in your form type because the parent
47+
of a form can change after building it.
4148

4249
### Routing
4350

src/Symfony/Bridge/Propel1/Form/EventListener/TranslationFormListener.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
namespace Symfony\Bridge\Propel1\Form\EventListener;
1212

1313
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
14-
use Symfony\Component\Form\FormFactoryInterface;
1514
use Symfony\Component\Form\FormEvent;
1615
use Symfony\Component\Form\FormEvents;
1716

@@ -24,13 +23,11 @@ class TranslationFormListener implements EventSubscriberInterface
2423
{
2524
private $columns;
2625
private $dataClass;
27-
private $formFactory;
2826

29-
public function __construct(FormFactoryInterface $formFactory, $columns, $dataClass)
27+
public function __construct($columns, $dataClass)
3028
{
3129
$this->columns = $columns;
3230
$this->dataClass = $dataClass;
33-
$this->formFactory = $formFactory;
3431
}
3532

3633
public static function getSubscribedEvents()
@@ -78,7 +75,7 @@ public function preSetData(FormEvent $event)
7875

7976
$options = array_merge($options, $customOptions);
8077

81-
$form->add($this->formFactory->createNamed($column, $type, null, $options));
78+
$form->add($column, $type, $options);
8279
}
8380
}
8481
}

src/Symfony/Bridge/Propel1/Form/Type/TranslationType.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,9 @@ class TranslationType extends AbstractType
2828
*/
2929
public function buildForm(FormBuilderInterface $builder, array $options)
3030
{
31-
$listener = new TranslationFormListener($builder->getFormFactory(), $options['columns'], $options['data_class']);
32-
$builder->addEventSubscriber($listener);
31+
$builder->addEventSubscriber(
32+
new TranslationFormListener($options['columns'], $options['data_class'])
33+
);
3334
}
3435

3536
/**

src/Symfony/Component/Form/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ CHANGELOG
55
-----
66

77
* TrimListener now removes unicode whitespaces
8+
* deprecated getParent(), setParent() and hasParent() in FormBuilderInterface
9+
* FormInterface::add() now accepts a FormInterface instance OR a field's name, type and options
810

911
2.1.0
1012
-----

src/Symfony/Component/Form/Extension/Core/EventListener/ResizeFormListener.php

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313

1414
use Symfony\Component\Form\FormEvents;
1515
use Symfony\Component\Form\FormEvent;
16-
use Symfony\Component\Form\FormFactoryInterface;
1716
use Symfony\Component\Form\Exception\UnexpectedTypeException;
1817
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
1918

@@ -24,11 +23,6 @@
2423
*/
2524
class ResizeFormListener implements EventSubscriberInterface
2625
{
27-
/**
28-
* @var FormFactoryInterface
29-
*/
30-
protected $factory;
31-
3226
/**
3327
* @var string
3428
*/
@@ -51,9 +45,8 @@ class ResizeFormListener implements EventSubscriberInterface
5145
*/
5246
protected $allowDelete;
5347

54-
public function __construct(FormFactoryInterface $factory, $type, array $options = array(), $allowAdd = false, $allowDelete = false)
48+
public function __construct($type, array $options = array(), $allowAdd = false, $allowDelete = false)
5549
{
56-
$this->factory = $factory;
5750
$this->type = $type;
5851
$this->allowAdd = $allowAdd;
5952
$this->allowDelete = $allowDelete;
@@ -90,9 +83,9 @@ public function preSetData(FormEvent $event)
9083

9184
// Then add all rows again in the correct order
9285
foreach ($data as $name => $value) {
93-
$form->add($this->factory->createNamed($name, $this->type, null, array_replace(array(
86+
$form->add((string) $name, $this->type, array_replace(array(
9487
'property_path' => '['.$name.']',
95-
), $this->options)));
88+
), $this->options));
9689
}
9790
}
9891

@@ -122,9 +115,9 @@ public function preBind(FormEvent $event)
122115
if ($this->allowAdd) {
123116
foreach ($data as $name => $value) {
124117
if (!$form->has($name)) {
125-
$form->add($this->factory->createNamed($name, $this->type, null, array_replace(array(
118+
$form->add((string) $name, $this->type, array_replace(array(
126119
'property_path' => '['.$name.']',
127-
), $this->options)));
120+
), $this->options));
128121
}
129122
}
130123
}

src/Symfony/Component/Form/Extension/Core/Type/CollectionType.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ public function buildForm(FormBuilderInterface $builder, array $options)
3434
}
3535

3636
$resizeListener = new ResizeFormListener(
37-
$builder->getFormFactory(),
3837
$options['type'],
3938
$options['options'],
4039
$options['allow_add'],

src/Symfony/Component/Form/Form.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\Form;
1313

1414
use Symfony\Component\Form\Exception\FormException;
15+
use Symfony\Component\Form\Exception\UnexpectedTypeException;
1516
use Symfony\Component\Form\Exception\AlreadyBoundException;
1617
use Symfony\Component\Form\Exception\TransformationFailedException;
1718
use Symfony\Component\Form\Util\FormUtil;
@@ -859,7 +860,7 @@ public function hasChildren()
859860
/**
860861
* {@inheritdoc}
861862
*/
862-
public function add(FormInterface $child)
863+
public function add($child, $type = null, array $options = array())
863864
{
864865
if ($this->bound) {
865866
throw new AlreadyBoundException('You cannot add children to a bound form');
@@ -888,6 +889,22 @@ public function add(FormInterface $child)
888889
$viewData = $this->getViewData();
889890
}
890891

892+
if (!$child instanceof FormInterface) {
893+
if (!is_string($child)) {
894+
throw new UnexpectedTypeException($child, 'string or Symfony\Component\Form\FormInterface');
895+
}
896+
897+
if (null !== $type && !is_string($type) && !$type instanceof FormTypeInterface) {
898+
throw new UnexpectedTypeException($type, 'string or Symfony\Component\Form\FormTypeInterface');
899+
}
900+
901+
if (null === $type) {
902+
$child = $this->config->getFormFactory()->createForProperty($this->config->getDataClass(), $child, null, $options);
903+
} else {
904+
$child = $this->config->getFormFactory()->createNamed($child, $type, null, $options);
905+
}
906+
}
907+
891908
$this->children[$child->getName()] = $child;
892909

893910
$child->setParent($this);

src/Symfony/Component/Form/FormBuilder.php

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,6 @@
2222
*/
2323
class FormBuilder extends FormConfigBuilder implements \IteratorAggregate, FormBuilderInterface
2424
{
25-
/**
26-
* The form factory.
27-
*
28-
* @var FormFactoryInterface
29-
*/
30-
private $factory;
31-
3225
/**
3326
* The children of the form builder.
3427
*
@@ -63,15 +56,7 @@ public function __construct($name, $dataClass, EventDispatcherInterface $dispatc
6356
{
6457
parent::__construct($name, $dataClass, $dispatcher, $options);
6558

66-
$this->factory = $factory;
67-
}
68-
69-
/**
70-
* {@inheritdoc}
71-
*/
72-
public function getFormFactory()
73-
{
74< 10000 code>-
return $this->factory;
59+
$this->setFormFactory($factory);
7560
}
7661

7762
/**
@@ -125,10 +110,10 @@ public function create($name, $type = null, array $options = array())
125110
}
126111

127112
if (null !== $type) {
128-
return $this->factory->createNamedBuilder($name, $type, null, $options, $this);
113+
return $this->getFormFactory()->createNamedBuilder($name, $type, null, $options, $this);
129114
}
130115

131-
return $this->factory->createBuilderForProperty($this->getDataClass(), $name, null, $options, $this);
116+
return $this->getFormFactory()->createBuilderForProperty($this->getDataClass(), $name, null, $options, $this);
132117
}
133118

134119
/**

src/Symfony/Component/Form/FormBuilderInterface.php

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ public function create($name, $type = null, array $options = array());
5252
* @throws Exception\FormException if the given child does not exist
5353
*/
5454
public function get($name);
55+
5556
/**
5657
* Removes the field with the given name.
5758
*
@@ -77,13 +78,6 @@ public function has($name);
7778
*/
7879
public function all();
7980

80-
/**
81-
* Returns the associated form factory.
82-
*
83-
* @return FormFactoryInterface The factory
84-
*/
85-
public function getFormFactory();
86-
8781
/**
8882
* Creates the form.
8983
*
@@ -97,20 +91,35 @@ public function getForm();
9791
* @param FormBuilderInterface $parent The parent builder
9892
*
9993
* @return FormBuilderInterface The builder object.
94+
*
95+
* @deprecated Deprecated since version 2.2, to be removed in 2.3. You
96+
* should not rely on the parent of a builder, because it is
97+
* likely that the parent is only set after turning the builder
98+
* into a form.
10099
*/
101100
public function setParent(FormBuilderInterface $parent = null);
102101

103102
/**
104103
* Returns the parent builder.
105104
*
106105
* @return FormBuilderInterface The parent builder
106+
*
107+
* @deprecated Deprecated since version 2.2, to be removed in 2.3. You
108+
* should not rely on the parent of a builder, because it is
109+
* likely that the parent is only set after turning the builder
110+
* into a form.
107111
*/
108112
public function getParent();
109113

110114
/**
111115
* Returns whether the builder has a parent.
112116
*
113117
* @return Boolean
118+
*
119+
* @deprecated Deprecated since version 2.2, to be removed in 2.3. You
120+
* should not rely on the parent of a builder, because it is
121+
* likely that the parent is only set after turning the builder
122+
* into a form.
114123
*/
115124
public function hasParent();
116125
}

src/Symfony/Component/Form/FormConfigBuilder.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,11 @@ class FormConfigBuilder implements FormConfigBuilderInterface
131131
*/
132132
private $dataLocked;
133133

134+
/**
135+
* @var FormFactoryInterface
136+
*/
137+
p 10000 rivate $formFactory;
138+
134139
/**
135140
* @var array
136141
*/
@@ -611,6 +616,14 @@ public function getDataLocked()
611616
return $this->dataLocked;
612617
}
613618

619+
/**
620+
* {@inheritdoc}
621+
*/
622+
public function getFormFactory()
623+
{
624+
return $this->formFactory;
625+
}
626+
614627
/**
615628
* {@inheritdoc}
616629
*/
@@ -849,6 +862,20 @@ public function setDataLocked($locked)
849862
return $this;
850863
}
851864

865+
/**
866+
* {@inheritdoc}
867+
*/
868+
public function setFormFactory(FormFactoryInterface $formFactory)
869+
{
870+
if ($this->locked) {
871+
throw new FormException('The config builder cannot be modified anymore.');
872+
}
873+
874+
$this->formFactory = $formFactory;
875+
876+
return $this;
877+
}
878+
852879
/**
853880
* {@inheritdoc}
854881
*/

src/Symfony/Component/Form/FormConfigBuilderInterface.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,13 @@ public function setData($data);
241241
*/
242242
public function setDataLocked($locked);
243243

244+
/**
245+
* Sets the form factory used for creating new forms.
246+
*
247+
* @param FormFactoryInterface $formFactory The form factory.
248+
*/
249+
public function setFormFactory(FormFactoryInterface $formFactory);
250+
244251
/**
245252
* Builds and returns the form configuration.
246253
*

src/Symfony/Component/Form/FormConfigInterface.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,13 @@ public function getDataClass();
192192
*/
193193
public function getDataLocked();
194194

195+
/**
196+
* Returns the form factory used for creating new forms.
197+
*
198+
* @return FormFactoryInterface The form factory.
199+
*/
200+
public function getFormFactory();
201+
195202
/**
196203
* Returns all options passed during the construction of the form.
197204
*

src/Symfony/Component/Form/FormInterface.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,17 @@ public function getParent();
4141
/**
4242
* Adds a child to the form.
4343
*
44-
* @param FormInterface $child The FormInterface to add as a child
44+
* @param FormInterface|string $child The FormInterface instance or the name of the child.
45+
* @param string|null $type The child's type, if a name was passed.
46+
* @param array $options The child's options, if a name was passed.
4547
*
4648
* @return FormInterface The form instance
4749
*
48-
* @throws Exception\AlreadyBoundException If the form has already been bound.
49-
* @throws Exception\FormException When trying to add a child to a non-compound form.
50+
* @throws Exception\AlreadyBoundException If the form has already been bound.
51+
* @throws Exception\FormException When trying to add a child to a non-compound form.
52+
* @throws Exception\UnexpectedTypeException If $child or $type has an unexpected type.
5053
*/
51-
public function add(FormInterface $child);
54+
public function add($child, $type = null, array $options = array());
5255

5356
/**
5457
* Returns the child with the given name.

0 commit comments

Comments
 (0)
0