|
0
|
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; |
|
|
13 |
|
|
|
14 |
use Symfony\Component\Form\Exception\FormException; |
|
|
15 |
use Symfony\Component\Form\Exception\UnexpectedTypeException; |
|
|
16 |
use Symfony\Component\Form\Exception\CircularReferenceException; |
|
|
17 |
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
|
|
18 |
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
|
|
19 |
|
|
|
20 |
class FormBuilder |
|
|
21 |
{ |
|
|
22 |
/** |
|
|
23 |
* @var string |
|
|
24 |
*/ |
|
|
25 |
private $name; |
|
|
26 |
|
|
|
27 |
/** |
|
|
28 |
* The form data in application format |
|
|
29 |
* @var mixed |
|
|
30 |
*/ |
|
|
31 |
private $appData; |
|
|
32 |
|
|
|
33 |
/** |
|
|
34 |
* The event dispatcher |
|
|
35 |
* |
|
|
36 |
* @var EventDispatcherInterface |
|
|
37 |
*/ |
|
|
38 |
private $dispatcher; |
|
|
39 |
|
|
|
40 |
/** |
|
|
41 |
* The form factory |
|
|
42 |
* @var FormFactoryInterface |
|
|
43 |
*/ |
|
|
44 |
private $factory; |
|
|
45 |
|
|
|
46 |
/** |
|
|
47 |
* @var Boolean |
|
|
48 |
*/ |
|
|
49 |
private $readOnly; |
|
|
50 |
|
|
|
51 |
/** |
|
|
52 |
* @var Boolean |
|
|
53 |
*/ |
|
|
54 |
private $required; |
|
|
55 |
|
|
|
56 |
/** |
|
|
57 |
* The transformers for transforming from normalized to client format and |
|
|
58 |
* back |
|
|
59 |
* @var array An array of DataTransformerInterface |
|
|
60 |
*/ |
|
|
61 |
private $clientTransformers = array(); |
|
|
62 |
|
|
|
63 |
/** |
|
|
64 |
* The transformers for transforming from application to normalized format |
|
|
65 |
* and back |
|
|
66 |
* @var array An array of DataTransformerInterface |
|
|
67 |
*/ |
|
|
68 |
private $normTransformers = array(); |
|
|
69 |
|
|
|
70 |
/** |
|
|
71 |
* @var array An array of FormValidatorInterface |
|
|
72 |
*/ |
|
|
73 |
private $validators = array(); |
|
|
74 |
|
|
|
75 |
/** |
|
|
76 |
* Key-value store for arbitrary attributes attached to the form |
|
|
77 |
* @var array |
|
|
78 |
*/ |
|
|
79 |
private $attributes = array(); |
|
|
80 |
|
|
|
81 |
/** |
|
|
82 |
* @var array An array of FormTypeInterface |
|
|
83 |
*/ |
|
|
84 |
private $types = array(); |
|
|
85 |
|
|
|
86 |
/** |
|
|
87 |
* @var string |
|
|
88 |
*/ |
|
|
89 |
private $dataClass; |
|
|
90 |
|
|
|
91 |
/** |
|
|
92 |
* The children of the form |
|
|
93 |
* @var array |
|
|
94 |
*/ |
|
|
95 |
private $children = array(); |
|
|
96 |
|
|
|
97 |
/** |
|
|
98 |
* @var DataMapperInterface |
|
|
99 |
*/ |
|
|
100 |
private $dataMapper; |
|
|
101 |
|
|
|
102 |
/** |
|
|
103 |
* Whether added errors should bubble up to the parent |
|
|
104 |
* @var Boolean |
|
|
105 |
*/ |
|
|
106 |
private $errorBubbling = false; |
|
|
107 |
|
|
|
108 |
/** |
|
|
109 |
* Data used for the client data when no value is bound |
|
|
110 |
* @var mixed |
|
|
111 |
*/ |
|
|
112 |
private $emptyData = ''; |
|
|
113 |
|
|
|
114 |
private $currentLoadingType; |
|
|
115 |
|
|
|
116 |
/** |
|
|
117 |
* Constructor. |
|
|
118 |
* |
|
|
119 |
* @param string $name |
|
|
120 |
* @param FormFactoryInterface $factory |
|
|
121 |
* @param EventDispatcherInterface $dispatcher |
|
|
122 |
* @param string $dataClass |
|
|
123 |
*/ |
|
|
124 |
public function __construct($name, FormFactoryInterface $factory, EventDispatcherInterface $dispatcher, $dataClass = null) |
|
|
125 |
{ |
|
|
126 |
$this->name = $name; |
|
|
127 |
$this->factory = $factory; |
|
|
128 |
$this->dispatcher = $dispatcher; |
|
|
129 |
$this->dataClass = $dataClass; |
|
|
130 |
} |
|
|
131 |
|
|
|
132 |
/** |
|
|
133 |
* Returns the associated form factory. |
|
|
134 |
* |
|
|
135 |
* @return FormFactoryInterface The factory |
|
|
136 |
*/ |
|
|
137 |
public function getFormFactory() |
|
|
138 |
{ |
|
|
139 |
return $this->factory; |
|
|
140 |
} |
|
|
141 |
|
|
|
142 |
/** |
|
|
143 |
* Returns the name of the form. |
|
|
144 |
* |
|
|
145 |
* @return string The form name |
|
|
146 |
*/ |
|
|
147 |
public function getName() |
|
|
148 |
{ |
|
|
149 |
return $this->name; |
|
|
150 |
} |
|
|
151 |
|
|
|
152 |
/** |
|
|
153 |
* Updates the field with default data. |
|
|
154 |
* |
|
|
155 |
* @param array $appData The data formatted as expected for the underlying object |
|
|
156 |
* |
|
|
157 |
* @return FormBuilder The current builder |
|
|
158 |
*/ |
|
|
159 |
public function setData($appData) |
|
|
160 |
{ |
|
|
161 |
$this->appData = $appData; |
|
|
162 |
|
|
|
163 |
return $this; |
|
|
164 |
} |
|
|
165 |
|
|
|
166 |
/** |
|
|
167 |
* Returns the data in the format needed for the underlying object. |
|
|
168 |
* |
|
|
169 |
* @return mixed |
|
|
170 |
*/ |
|
|
171 |
public function getData() |
|
|
172 |
{ |
|
|
173 |
return $this->appData; |
|
|
174 |
} |
|
|
175 |
|
|
|
176 |
/** |
|
|
177 |
* Set whether the form is read only |
|
|
178 |
* |
|
|
179 |
* @param Boolean $readOnly Whether the form is read only |
|
|
180 |
* |
|
|
181 |
* @return FormBuilder The current builder |
|
|
182 |
*/ |
|
|
183 |
public function setReadOnly($readOnly) |
|
|
184 |
{ |
|
|
185 |
$this->readOnly = (Boolean) $readOnly; |
|
|
186 |
|
|
|
187 |
return $this; |
|
|
188 |
} |
|
|
189 |
|
|
|
190 |
/** |
|
|
191 |
* Returns whether the form is read only. |
|
|
192 |
* |
|
|
193 |
* @return Boolean Whether the form is read only |
|
|
194 |
*/ |
|
|
195 |
public function getReadOnly() |
|
|
196 |
{ |
|
|
197 |
return $this->readOnly; |
|
|
198 |
} |
|
|
199 |
|
|
|
200 |
/** |
|
|
201 |
* Sets whether this field is required to be filled out when bound. |
|
|
202 |
* |
|
|
203 |
* @param Boolean $required |
|
|
204 |
* |
|
|
205 |
* @return FormBuilder The current builder |
|
|
206 |
*/ |
|
|
207 |
public function setRequired($required) |
|
|
208 |
{ |
|
|
209 |
$this->required = (Boolean) $required; |
|
|
210 |
|
|
|
211 |
return $this; |
|
|
212 |
} |
|
|
213 |
|
|
|
214 |
/** |
|
|
215 |
* Returns whether this field is required to be filled out when bound. |
|
|
216 |
* |
|
|
217 |
* @return Boolean Whether this field is required |
|
|
218 |
*/ |
|
|
219 |
public function getRequired() |
|
|
220 |
{ |
|
|
221 |
return $this->required; |
|
|
222 |
} |
|
|
223 |
|
|
|
224 |
/** |
|
|
225 |
* Sets whether errors bubble up to the parent. |
|
|
226 |
* |
|
|
227 |
* @param type $errorBubbling |
|
|
228 |
* |
|
|
229 |
* @return FormBuilder The current builder |
|
|
230 |
*/ |
|
|
231 |
public function setErrorBubbling($errorBubbling) |
|
|
232 |
{ |
|
|
233 |
$this->errorBubbling = (Boolean) $errorBubbling; |
|
|
234 |
|
|
|
235 |
return $this; |
|
|
236 |
} |
|
|
237 |
|
|
|
238 |
/** |
|
|
239 |
* Returns whether errors bubble up to the parent. |
|
|
240 |
* |
|
|
241 |
* @return Boolean |
|
|
242 |
*/ |
|
|
243 |
public function getErrorBubbling() |
|
|
244 |
{ |
|
|
245 |
return $this->errorBubbling; |
|
|
246 |
} |
|
|
247 |
|
|
|
248 |
/** |
|
|
249 |
* Adds a validator to the form. |
|
|
250 |
* |
|
|
251 |
* @param FormValidatorInterface $validator The validator |
|
|
252 |
* |
|
|
253 |
* @return FormBuilder The current builder |
|
|
254 |
*/ |
|
|
255 |
public function addValidator(FormValidatorInterface $validator) |
|
|
256 |
{ |
|
|
257 |
$this->validators[] = $validator; |
|
|
258 |
|
|
|
259 |
return $this; |
|
|
260 |
} |
|
|
261 |
|
|
|
262 |
/** |
|
|
263 |
* Returns the validators used by the form. |
|
|
264 |
* |
|
|
265 |
* @return array An array of FormValidatorInterface |
|
|
266 |
*/ |
|
|
267 |
public function getValidators() |
|
|
268 |
{ |
|
|
269 |
return $this->validators; |
|
|
270 |
} |
|
|
271 |
|
|
|
272 |
/** |
|
|
273 |
* Adds an event listener for events on this field |
|
|
274 |
* |
|
|
275 |
* @see Symfony\Component\EventDispatcher\EventDispatcherInterface::addListener |
|
|
276 |
* |
|
|
277 |
* @return FormBuilder The current builder |
|
|
278 |
*/ |
|
|
279 |
public function addEventListener($eventName, $listener, $priority = 0) |
|
|
280 |
{ |
|
|
281 |
$this->dispatcher->addListener($eventName, $listener, $priority); |
|
|
282 |
|
|
|
283 |
return $this; |
|
|
284 |
} |
|
|
285 |
|
|
|
286 |
/** |
|
|
287 |
* Adds an event subscriber for events on this field |
|
|
288 |
* |
|
|
289 |
* @see Symfony\Component\EventDispatcher\EventDispatcherInterface::addSubscriber |
|
|
290 |
* |
|
|
291 |
* @return FormBuilder The current builder |
|
|
292 |
*/ |
|
|
293 |
public function addEventSubscriber(EventSubscriberInterface $subscriber) |
|
|
294 |
{ |
|
|
295 |
$this->dispatcher->addSubscriber($subscriber); |
|
|
296 |
|
|
|
297 |
return $this; |
|
|
298 |
} |
|
|
299 |
|
|
|
300 |
/** |
|
|
301 |
* Appends a transformer to the normalization transformer chain |
|
|
302 |
* |
|
|
303 |
* @param DataTransformerInterface $clientTransformer |
|
|
304 |
* |
|
|
305 |
* @return FormBuilder The current builder |
|
|
306 |
*/ |
|
|
307 |
public function appendNormTransformer(DataTransformerInterface $normTransformer) |
|
|
308 |
{ |
|
|
309 |
$this->normTransformers[] = $normTransformer; |
|
|
310 |
|
|
|
311 |
return $this; |
|
|
312 |
} |
|
|
313 |
|
|
|
314 |
/** |
|
|
315 |
* Prepends a transformer to the client transformer chain |
|
|
316 |
* |
|
|
317 |
* @param DataTransformerInterface $normTransformer |
|
|
318 |
* |
|
|
319 |
* @return FormBuilder The current builder |
|
|
320 |
*/ |
|
|
321 |
public function prependNormTransformer(DataTransformerInterface $normTransformer) |
|
|
322 |
{ |
|
|
323 |
array_unshift($this->normTransformers, $normTransformer); |
|
|
324 |
|
|
|
325 |
return $this; |
|
|
326 |
} |
|
|
327 |
|
|
|
328 |
/** |
|
|
329 |
* Clears the normalization transformers. |
|
|
330 |
* |
|
|
331 |
* @return FormBuilder The current builder |
|
|
332 |
*/ |
|
|
333 |
public function resetNormTransformers() |
|
|
334 |
{ |
|
|
335 |
$this->normTransformers = array(); |
|
|
336 |
|
|
|
337 |
return $this; |
|
|
338 |
} |
|
|
339 |
|
|
|
340 |
/** |
|
|
341 |
* Returns all the normalization transformers. |
|
|
342 |
* |
|
|
343 |
* @return array An array of DataTransformerInterface |
|
|
344 |
*/ |
|
|
345 |
public function getNormTransformers() |
|
|
346 |
{ |
|
|
347 |
return $this->normTransformers; |
|
|
348 |
} |
|
|
349 |
|
|
|
350 |
/** |
|
|
351 |
* Appends a transformer to the client transformer chain |
|
|
352 |
* |
|
|
353 |
* @param DataTransformerInterface $clientTransformer |
|
|
354 |
* |
|
|
355 |
* @return FormBuilder The current builder |
|
|
356 |
*/ |
|
|
357 |
public function appendClientTransformer(DataTransformerInterface $clientTransformer) |
|
|
358 |
{ |
|
|
359 |
$this->clientTransformers[] = $clientTransformer; |
|
|
360 |
|
|
|
361 |
return $this; |
|
|
362 |
} |
|
|
363 |
|
|
|
364 |
/** |
|
|
365 |
* Prepends a transformer to the client transformer chain |
|
|
366 |
* |
|
|
367 |
* @param DataTransformerInterface $clientTransformer |
|
|
368 |
* |
|
|
369 |
* @return FormBuilder The current builder |
|
|
370 |
*/ |
|
|
371 |
public function prependClientTransformer(DataTransformerInterface $clientTransformer) |
|
|
372 |
{ |
|
|
373 |
array_unshift($this->clientTransformers, $clientTransformer); |
|
|
374 |
|
|
|
375 |
return $this; |
|
|
376 |
} |
|
|
377 |
|
|
|
378 |
/** |
|
|
379 |
* Clears the client transformers. |
|
|
380 |
* |
|
|
381 |
* @return FormBuilder The current builder |
|
|
382 |
*/ |
|
|
383 |
public function resetClientTransformers() |
|
|
384 |
{ |
|
|
385 |
$this->clientTransformers = array(); |
|
|
386 |
|
|
|
387 |
return $this; |
|
|
388 |
} |
|
|
389 |
|
|
|
390 |
/** |
|
|
391 |
* Returns all the client transformers. |
|
|
392 |
* |
|
|
393 |
* @return array An array of DataTransformerInterface |
|
|
394 |
*/ |
|
|
395 |
public function getClientTransformers() |
|
|
396 |
{ |
|
|
397 |
return $this->clientTransformers; |
|
|
398 |
} |
|
|
399 |
|
|
|
400 |
/** |
|
|
401 |
* Sets the value for an attribute. |
|
|
402 |
* |
|
|
403 |
* @param string $name The name of the attribute |
|
|
404 |
* @param string $value The value of the attribute |
|
|
405 |
* |
|
|
406 |
* @return FormBuilder The current builder |
|
|
407 |
*/ |
|
|
408 |
public function setAttribute($name, $value) |
|
|
409 |
{ |
|
|
410 |
$this->attributes[$name] = $value; |
|
|
411 |
|
|
|
412 |
return $this; |
|
|
413 |
} |
|
|
414 |
|
|
|
415 |
/** |
|
|
416 |
* Returns the value of the attributes with the given name. |
|
|
417 |
* |
|
|
418 |
* @param string $name The name of the attribute |
|
|
419 |
*/ |
|
|
420 |
public function getAttribute($name) |
|
|
421 |
{ |
|
|
422 |
return $this->attributes[$name]; |
|
|
423 |
} |
|
|
424 |
|
|
|
425 |
/** |
|
|
426 |
* Returns whether the form has an attribute with the given name. |
|
|
427 |
* |
|
|
428 |
* @param string $name The name of the attribute |
|
|
429 |
*/ |
|
|
430 |
public function hasAttribute($name) |
|
|
431 |
{ |
|
|
432 |
return isset($this->attributes[$name]); |
|
|
433 |
} |
|
|
434 |
|
|
|
435 |
/** |
|
|
436 |
* Returns all the attributes. |
|
|
437 |
* |
|
|
438 |
* @return array An array of attributes |
|
|
439 |
*/ |
|
|
440 |
public function getAttributes() |
|
|
441 |
{ |
|
|
442 |
return $this->attributes; |
|
|
443 |
} |
|
|
444 |
|
|
|
445 |
/** |
|
|
446 |
* Sets the data mapper used by the form. |
|
|
447 |
* |
|
|
448 |
* @param DataMapperInterface $dataMapper |
|
|
449 |
* |
|
|
450 |
* @return FormBuilder The current builder |
|
|
451 |
*/ |
|
|
452 |
public function setDataMapper(DataMapperInterface $dataMapper) |
|
|
453 |
{ |
|
|
454 |
$this->dataMapper = $dataMapper; |
|
|
455 |
|
|
|
456 |
return $this; |
|
|
457 |
} |
|
|
458 |
|
|
|
459 |
/** |
|
|
460 |
* Returns the data mapper used by the form. |
|
|
461 |
* |
|
|
462 |
* @return array An array of DataMapperInterface |
|
|
463 |
*/ |
|
|
464 |
public function getDataMapper() |
|
|
465 |
{ |
|
|
466 |
return $this->dataMapper; |
|
|
467 |
} |
|
|
468 |
|
|
|
469 |
/** |
|
|
470 |
* Set the types. |
|
|
471 |
* |
|
|
472 |
* @param array $types An array FormTypeInterface |
|
|
473 |
* |
|
|
474 |
* @return FormBuilder The current builder |
|
|
475 |
*/ |
|
|
476 |
public function setTypes(array $types) |
|
|
477 |
{ |
|
|
478 |
$this->types = $types; |
|
|
479 |
|
|
|
480 |
return $this; |
|
|
481 |
} |
|
|
482 |
|
|
|
483 |
/** |
|
|
484 |
* Return the types. |
|
|
485 |
* |
|
|
486 |
* @return array An array of FormTypeInterface |
|
|
487 |
*/ |
|
|
488 |
public function getTypes() |
|
|
489 |
{ |
|
|
490 |
return $this->types; |
|
|
491 |
} |
|
|
492 |
|
|
|
493 |
/** |
|
|
494 |
* Sets the data used for the client data when no value is bound. |
|
|
495 |
* |
|
|
496 |
* @param mixed $emptyData |
|
|
497 |
*/ |
|
|
498 |
public function setEmptyData($emptyData) |
|
|
499 |
{ |
|
|
500 |
$this->emptyData = $emptyData; |
|
|
501 |
|
|
|
502 |
return $this; |
|
|
503 |
} |
|
|
504 |
|
|
|
505 |
/** |
|
|
506 |
* Returns the data used for the client data when no value is bound. |
|
|
507 |
* |
|
|
508 |
* @return mixed |
|
|
509 |
*/ |
|
|
510 |
public function getEmptyData() |
|
|
511 |
{ |
|
|
512 |
return $this->emptyData; |
|
|
513 |
} |
|
|
514 |
|
|
|
515 |
/** |
|
|
516 |
* Adds a new field to this group. A field must have a unique name within |
|
|
517 |
* the group. Otherwise the existing field is overwritten. |
|
|
518 |
* |
|
|
519 |
* If you add a nested group, this group should also be represented in the |
|
|
520 |
* object hierarchy. |
|
|
521 |
* |
|
|
522 |
* @param string|FormBuilder $child |
|
|
523 |
* @param string|FormTypeInterface $type |
|
|
524 |
* @param array $options |
|
|
525 |
* |
|
|
526 |
* @return FormBuilder The current builder |
|
|
527 |
*/ |
|
|
528 |
public function add($child, $type = null, array $options = array()) |
|
|
529 |
{ |
|
|
530 |
if ($child instanceof self) { |
|
|
531 |
$this->children[$child->getName()] = $child; |
|
|
532 |
|
|
|
533 |
return $this; |
|
|
534 |
} |
|
|
535 |
|
|
|
536 |
if (!is_string($child)) { |
|
|
537 |
throw new UnexpectedTypeException($child, 'string or Symfony\Component\Form\FormBuilder'); |
|
|
538 |
} |
|
|
539 |
|
|
|
540 |
if (null !== $type && !is_string($type) && !$type instanceof FormTypeInterface) { |
|
|
541 |
throw new UnexpectedTypeException($type, 'string or Symfony\Component\Form\FormTypeInterface'); |
|
|
542 |
} |
|
|
543 |
|
|
|
544 |
if ($this->currentLoadingType && ($type instanceof FormTypeInterface ? $type->getName() : $type) == $this->currentLoadingType) { |
|
|
545 |
throw new CircularReferenceException(is_string($type) ? $this->getFormFactory()->getType($type) : $type); |
|
|
546 |
} |
|
|
547 |
|
|
|
548 |
$this->children[$child] = array( |
|
|
549 |
'type' => $type, |
|
|
550 |
'options' => $options, |
|
|
551 |
); |
|
|
552 |
|
|
|
553 |
return $this; |
|
|
554 |
} |
|
|
555 |
|
|
|
556 |
/** |
|
|
557 |
* Creates a form builder. |
|
|
558 |
* |
|
|
559 |
* @param string $name The name of the form or the name of the property |
|
|
560 |
* @param string|FormTypeInterface $type The type of the form or null if name is a property |
|
|
561 |
* @param array $options The options |
|
|
562 |
* |
|
|
563 |
* @return FormBuilder The builder |
|
|
564 |
*/ |
|
|
565 |
public function create($name, $type = null, array $options = array()) |
|
|
566 |
{ |
|
|
567 |
if (null === $type && !$this->dataClass) { |
|
|
568 |
$type = 'text'; |
|
|
569 |
} |
|
|
570 |
|
|
|
571 |
if (null !== $type) { |
|
|
572 |
return $this->getFormFactory()->createNamedBuilder($type, $name, null, $options); |
|
|
573 |
} |
|
|
574 |
|
|
|
575 |
return $this->getFormFactory()->createBuilderForProperty($this->dataClass, $name, null, $options); |
|
|
576 |
} |
|
|
577 |
|
|
|
578 |
/** |
|
|
579 |
* Returns a child by name. |
|
|
580 |
* |
|
|
581 |
* @param string $name The name of the child |
|
|
582 |
* |
|
|
583 |
* @return FormBuilder The builder for the child |
|
|
584 |
* |
|
|
585 |
* @throws FormException if the given child does not exist |
|
|
586 |
*/ |
|
|
587 |
public function get($name) |
|
|
588 |
{ |
|
|
589 |
if (!isset($this->children[$name])) { |
|
|
590 |
throw new FormException(sprintf('The field "%s" does not exist', $name)); |
|
|
591 |
} |
|
|
592 |
|
|
|
593 |
if (!$this->children[$name] instanceof FormBuilder) { |
|
|
594 |
$this->children[$name] = $this->create( |
|
|
595 |
$name, |
|
|
596 |
$this->children[$name]['type'], |
|
|
597 |
$this->children[$name]['options'] |
|
|
598 |
); |
|
|
599 |
} |
|
|
600 |
|
|
|
601 |
return $this->children[$name]; |
|
|
602 |
} |
|
|
603 |
|
|
|
604 |
/** |
|
|
605 |
* Removes the field with the given name. |
|
|
606 |
* |
|
|
607 |
* @param string $name |
|
|
608 |
* |
|
|
609 |
* @return FormBuilder The current builder |
|
|
610 |
*/ |
|
|
611 |
public function remove($name) |
|
|
612 |
{ |
|
|
613 |
if (isset($this->children[$name])) { |
|
|
614 |
unset($this->children[$name]); |
|
|
615 |
} |
|
|
616 |
|
|
|
617 |
return $this; |
|
|
618 |
} |
|
|
619 |
|
|
|
620 |
/** |
|
|
621 |
* Returns whether a field with the given name exists. |
|
|
622 |
* |
|
|
623 |
* @param string $name |
|
|
624 |
* |
|
|
625 |
* @return Boolean |
|
|
626 |
*/ |
|
|
627 |
public function has($name) |
|
|
628 |
{ |
|
|
629 |
return isset($this->children[$name]); |
|
|
630 |
} |
|
|
631 |
|
|
|
632 |
/** |
|
|
633 |
* Creates the form. |
|
|
634 |
* |
|
|
635 |
* @return Form The form |
|
|
636 |
*/ |
|
|
637 |
public function getForm() |
|
|
638 |
{ |
|
|
639 |
$instance = new Form( |
|
|
640 |
$this->getName(), |
|
|
641 |
$this->buildDispatcher(), |
|
|
642 |
$this->getTypes(), |
|
|
643 |
$this->getClientTransformers(), |
|
|
644 |
$this->getNormTransformers(), |
|
|
645 |
$this->getDataMapper(), |
|
|
646 |
$this->getValidators(), |
|
|
647 |
$this->getRequired(), |
|
|
648 |
$this->getReadOnly(), |
|
|
649 |
$this->getErrorBubbling(), |
|
|
650 |
$this->getEmptyData(), |
|
|
651 |
$this->getAttributes() |
|
|
652 |
); |
|
|
653 |
|
|
|
654 |
foreach ($this->buildChildren() as $child) { |
|
|
655 |
$instance->add($child); |
|
|
656 |
} |
|
|
657 |
|
|
|
658 |
if (null !== $this->getData()) { |
|
|
659 |
$instance->setData($this->getData()); |
|
|
660 |
} |
|
|
661 |
|
|
|
662 |
return $instance; |
|
|
663 |
} |
|
|
664 |
|
|
|
665 |
public function setCurrentLoadingType($type) |
|
|
666 |
{ |
|
|
667 |
$this->currentLoadingType = $type; |
|
|
668 |
} |
|
|
669 |
|
|
|
670 |
/** |
|
|
671 |
* Returns the event dispatcher. |
|
|
672 |
* |
|
|
673 |
* @return type |
|
|
674 |
*/ |
|
|
675 |
protected function buildDispatcher() |
|
|
676 |
{ |
|
|
677 |
return $this->dispatcher; |
|
|
678 |
} |
|
|
679 |
|
|
|
680 |
/** |
|
|
681 |
* Creates the children. |
|
|
682 |
* |
|
|
683 |
* @return array An array of Form |
|
|
684 |
*/ |
|
|
685 |
protected function buildChildren() |
|
|
686 |
{ |
|
|
687 |
$children = array(); |
|
|
688 |
|
|
|
689 |
foreach ($this->children as $name => $builder) { |
|
|
690 |
if (!$builder instanceof FormBuilder) { |
|
|
691 |
$builder = $this->create($name, $builder['type'], $builder['options']); |
|
|
692 |
} |
|
|
693 |
|
|
|
694 |
$children[$builder->getName()] = $builder->getForm(); |
|
|
695 |
} |
|
|
696 |
|
|
|
697 |
return $children; |
|
|
698 |
} |
|
|
699 |
} |