Skip to content

Commit bcc5552

Browse files
committed
[Form] Protected methods in FormConfigBuilder and FormBuilder from being called when it is turned into a FormConfigInterface instance
1 parent fee1bf5 commit bcc5552

File tree

5 files changed

+142
-67
lines changed

5 files changed

+142
-67
lines changed

UPGRADE-2.2.md

+7
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,13 @@
5757

5858
* The PasswordType is now not trimmed by default.
5959

60+
* The class FormException is now an interface. The old class is still available
61+
under the name Symfony\Component\Form\Exception\Exception, but will probably
62+
be removed before 2.2. If you created FormException instances manually,
63+
you are now advised to create any of the other exceptions in the
64+
Symfony\Component\Form\Exception namespace or to create custom exception
65+
classes for your purpose.
66+
6067
#### Deprecations
6168

6269
* The methods `getParent()`, `setParent()` and `hasParent()` in

src/Symfony/Component/Form/CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ CHANGELOG
1010
* removed special characters between the choice or text fields of DateType unless
1111
the option "format" is set to a custom value
1212
* deprecated FormException and introduced ExceptionInterface instead
13+
* [BC BREAK] FormException is now an interface
14+
* protected FormBuilder methods from being called when it is turned into a FormConfigInterface with getFormConfig()
1315

1416
2.1.0
1517
-----
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Form\Exception;
13+
14+
/**
15+
* Base BadMethodCallException for the Form component.
16+
*
17+
* @author Bernhard Schussek <bschussek@gmail.com>
18+
*/
19+
class BadMethodCallException extends \BadMethodCallException implements ExceptionInterface
20+
{
21+
}

src/Symfony/Component/Form/FormBuilder.php

+72-32
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Component\Form;
1313

14+
use Symfony\Component\Form\Exception\BadMethodCallException;
1415
use Symfony\Component\Form\Exception\Exception;
1516
use Symfony\Component\Form\Exception\UnexpectedTypeException;
1617
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
@@ -65,7 +66,7 @@ public function __construct($name, $dataClass, EventDispatcherInterface $dispatc
6566
public function add($child, $type = null, array $options = array())
6667
{
6768
if ($this->locked) {
68-
throw new Exception('The form builder cannot be modified anymore.');
69+
throw new BadMethodCallException('FormBuilder methods cannot be accessed anymore once the builder is turned into a FormConfigInterface instance.');
6970
}
7071

7172
if ($child instanceof self) {
@@ -102,7 +103,7 @@ public function add($child, $type = null, array $options = array())
102103
public function create($name, $type = null, array $options = array())
103104
{
104105
if ($this->locked) {
105-
throw new Exception('The form builder cannot be modified anymore.');
106+
throw new BadMethodCallException('FormBuilder methods cannot be accessed anymore once the builder is turned into a FormConfigInterface instance.');
106107
}
107108

108109
if (null === $type && null === $this->getDataClass()) {
@@ -121,6 +122,10 @@ public function create($name, $type = null, array $options = array())
121122
*/
122123
public function get($name)
123124
{
125+
if ($this->locked) {
126+
throw new BadMethodCallException('FormBuilder methods cannot be accessed anymore once the builder is turned into a FormConfigInterface instance.');
127+
}
128+
124129
if (isset($this->unresolvedChildren[$name])) {
125130
return $this->resolveChild($name);
126131
}
@@ -138,7 +143,7 @@ public function get($name)
138143
public function remove($name)
139144
{
140145
if ($this->locked) {
141-
throw new Exception('The form builder cannot be modified anymore.');
146+
throw new BadMethodCallException('FormBuilder methods cannot be accessed anymore once the builder is turned into a FormConfigInterface instance.');
142147
}
143148

144149
unset($this->unresolvedChildren[$name]);
@@ -158,6 +163,10 @@ public function remove($name)
158163
*/
159164
public function has($name)
160165
{
166+
if ($this->locked) {
167+
throw new BadMethodCallException('FormBuilder methods cannot be accessed anymore once the builder is turned into a FormConfigInterface instance.');
168+
}
169+
161170
if (isset($this->unresolvedChildren[$name])) {
162171
return true;
163172
}
@@ -174,6 +183,10 @@ public function has($name)
174183
*/
175184
public function all()
176185
{
186+
if ($this->locked) {
187+
throw new BadMethodCallException('FormBuilder methods cannot be accessed anymore once the builder is turned into a FormConfigInterface instance.');
188+
}
189+
177190
$this->resolveChildren();
178191

179192
return $this->children;
@@ -184,6 +197,10 @@ public function all()
184197
*/
185198
public function count()
186199
{
200+
if ($this->locked) {
201+
throw new BadMethodCallException('FormBuilder methods cannot be accessed anymore once the builder is turned into a FormConfigInterface instance.');
202+
}
203+
187204
return count($this->children);
188205
}
189206

@@ -192,6 +209,10 @@ public function count()
192209
*/
193210
public function getForm()
194211
{
212+
if ($this->locked) {
213+
throw new BadMethodCallException('FormBuilder methods cannot be accessed anymore once the builder is turned into a FormConfigInterface instance.');
214+
}
215+
195216
$this->resolveChildren();
196217

197218
$form = new Form($this->getFormConfig());
@@ -208,6 +229,10 @@ public function getForm()
208229
*/
209230
public function getParent()
210231
{
232+
if ($this->locked) {
233+
throw new BadMethodCallException('FormBuilder methods cannot be accessed anymore once the builder is turned into a FormConfigInterface instance.');
234+
}
235+
211236
return $this->parent;
212237
}
213238

@@ -217,7 +242,7 @@ public function getParent()
217242
public function setParent(FormBuilderInterface $parent = null)
218243
{
219244
if ($this->locked) {
220-
throw new Exception('The form builder cannot be modified anymore.');
245+
throw new BadMethodCallException('FormBuilder methods cannot be accessed anymore once the builder is turned into a FormConfigInterface instance.');
221246
}
222247

223248
$this->parent = $parent;
@@ -230,43 +255,22 @@ public function setParent(FormBuilderInterface $parent = null)
230255
*/
231256
public function hasParent()
232257
{
233-
return null !== $this->parent;
234-
}
235-
236-
/**
237-
* Converts an unresolved child into a {@link FormBuilder} instance.
238-
*
239-
* @param string $name The name of the unresolved child.
240-
*
241-
* @return FormBuilder The created instance.
242-
*/
243-
private function resolveChild($name)
244-
{
245-
$info = $this->unresolvedChildren[$name];
246-
$child = $this->create($name, $info['type'], $info['options']);
247-
$this->children[$name] = $child;
248-
unset($this->unresolvedChildren[$name]);
249-
250-
return $child;
251-
}
252-
253-
/**
254-
* Converts all unresolved children into {@link FormBuilder} instances.
255-
*/
256-
private function resolveChildren()
257-
{
258-
foreach ($this->unresolvedChildren as $name => $info) {
259-
$this->children[$name] = $this->create($name, $info['type'], $info['options']);
258+
if ($this->locked) {
259+
throw new BadMethodCallException('FormBuilder methods cannot be accessed anymore once the builder is turned into a FormConfigInterface instance.');
260260
}
261261

262-
$this->unresolvedChildren = array();
262+
return null !== $this->parent;
263263
}
264264

265265
/**
266266
* {@inheritdoc}
267267
*/
268268
public function getIterator()
269269
{
270+
if ($this->locked) {
271+
throw new BadMethodCallException('FormBuilder methods cannot be accessed anymore once the builder is turned into a FormConfigInterface instance.');
272+
}
273+
270274
return new \ArrayIterator($this->children);
271275
}
272276

@@ -277,9 +281,16 @@ public function getIterator()
277281
*
278282
* @deprecated Deprecated since version 2.1, to be removed in 2.3. Use
279283
* {@link FormConfigInterface::getType()} instead.
284+
*
285+
* @throws BadMethodCallException If the builder was turned into a {@link FormConfigInterface}
286+
* via {@link getFormConfig()}.
280287
*/
281288
public function getTypes()
282289
{
290+
if ($this->locked) {
291+
throw new BadMethodCallException('FormBuilder methods cannot be accessed anymore once the builder is turned into a FormConfigInterface instance.');
292+
}
293+
283294
trigger_error('getTypes() is deprecated since version 2.1 and will be removed in 2.3. Use getConfig() and FormConfigInterface::getType() instead.', E_USER_DEPRECATED);
284295

285296
$types = array();
@@ -290,4 +301,33 @@ public function getTypes()
290301

291302
return $types;
292303
}
304+
305+
/**
306+
* Converts an unresolved child into a {@link FormBuilder} instance.
307+
*
308+
* @param string $name The name of the unresolved child.
309+
*
310+
* @return FormBuilder The created instance.
311+
*/
312+
private function resolveChild($name)
313+
{
314+
$info = $this->unresolvedChildren[$name];
315+
$child = $this->create($name, $info['type'], $info['options']);
316+
$this->children[$name] = $child;
317+
unset($this->unresolvedChildren[$name]);
318+
319+
return $child;
320+
}
321+
322+
/**
323+
* Converts all unresolved children into {@link FormBuilder} instances.
324+
*/
325+
private function resolveChildren()
326+
{
327+
foreach ($this->unresolvedChildren as $name => $info) {
328+
$this->children[$name] = $this->create($name, $info['type'], $info['options']);
329+
}
330+
331+
$this->unresolvedChildren = array();
332+
}
293333
}

0 commit comments

Comments
 (0)