11
11
12
12
namespace Symfony \Component \Form ;
13
13
14
+ use Symfony \Component \Form \Exception \BadMethodCallException ;
14
15
use Symfony \Component \Form \Exception \Exception ;
15
16
use Symfony \Component \Form \Exception \UnexpectedTypeException ;
16
17
use Symfony \Component \EventDispatcher \EventDispatcherInterface ;
@@ -65,7 +66,7 @@ public function __construct($name, $dataClass, EventDispatcherInterface $dispatc
65
66
public function add ($ child , $ type = null , array $ options = array ())
66
67
{
67
68
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 . ' );
69
70
}
70
71
71
72
if ($ child instanceof self) {
@@ -102,7 +103,7 @@ public function add($child, $type = null, array $options = array())
102
103
public function create ($ name , $ type = null , array $ options = array ())
103
104
{
104
105
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 . ' );
106
107
}
107
108
108
109
if (null === $ type && null === $ this ->getDataClass ()) {
@@ -121,6 +122,10 @@ public function create($name, $type = null, array $options = array())
121
122
*/
122
123
public function get ($ name )
123
124
{
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
+
124
129
if (isset ($ this ->unresolvedChildren [$ name ])) {
125
130
return $ this ->resolveChild ($ name );
126
131
}
@@ -138,7 +143,7 @@ public function get($name)
138
143
public function remove ($ name )
139
144
{
140
145
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 . ' );
142
147
}
143
148
144
149
unset($ this ->unresolvedChildren [$ name ]);
@@ -158,6 +163,10 @@ public function remove($name)
158
163
*/
159
164
public function has ($ name )
160
165
{
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
+
161
170
if (isset ($ this ->unresolvedChildren [$ name ])) {
162
171
return true ;
163
172
}
@@ -174,6 +183,10 @@ public function has($name)
174
183
*/
175
184
public function all ()
176
185
{
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
+
177
190
$ this ->resolveChildren ();
178
191
179
192
return $ this ->children ;
@@ -184,6 +197,10 @@ public function all()
184
197
*/
185
198
public function count ()
186
199
{
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
+
187
204
return count ($ this ->children );
188
205
}
189
206
@@ -192,6 +209,10 @@ public function count()
192
209
*/
193
210
public function getForm ()
194
211
{
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
+
195
216
$ this ->resolveChildren ();
196
217
197
218
$ form = new Form ($ this ->getFormConfig ());
@@ -208,6 +229,10 @@ public function getForm()
208
229
*/
209
230
public function getParent ()
210
231
{
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
+
211
236
return $ this ->parent ;
212
237
}
213
238
@@ -217,7 +242,7 @@ public function getParent()
217
242
public function setParent (FormBuilderInterface $ parent = null )
218
243
{
219
244
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 . ' );
221
246
}
222
247
223
248
$ this ->parent = $ parent ;
@@ -230,43 +255,22 @@ public function setParent(FormBuilderInterface $parent = null)
230
255
*/
231
256
public function hasParent ()
232
257
{
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. ' );
260
260
}
261
261
262
- $ this ->unresolvedChildren = array () ;
262
+ return null !== $ this ->parent ;
263
263
}
264
264
265
265
/**
266
266
* {@inheritdoc}
267
267
*/
268
268
public function getIterator ()
269
269
{
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
+
270
274
return new \ArrayIterator ($ this ->children );
271
275
}
272
276
@@ -277,9 +281,16 @@ public function getIterator()
277
281
*
278
282
* @deprecated Deprecated since version 2.1, to be removed in 2.3. Use
279
283
* {@link FormConfigInterface::getType()} instead.
284
+ *
285
+ * @throws BadMethodCallException If the builder was turned into a {@link FormConfigInterface}
286
+ * via {@link getFormConfig()}.
280
287
*/
281
288
public function getTypes ()
282
289
{
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
+
283
294
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 );
284
295
285
296
$ types = array ();
@@ -290,4 +301,33 @@ public function getTypes()
290
301
291
302
return $ types ;
292
303
}
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
+ }
293
333
}
0 commit comments