|
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\Event\DataEvent; |
|
|
15 |
use Symfony\Component\Form\Event\FilterDataEvent; |
|
|
16 |
use Symfony\Component\Form\Exception\FormException; |
|
|
17 |
use Symfony\Component\Form\Exception\UnexpectedTypeException; |
|
|
18 |
use Symfony\Component\Form\Exception\TransformationFailedException; |
|
|
19 |
use Symfony\Component\HttpFoundation\Request; |
|
|
20 |
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
|
|
21 |
|
|
|
22 |
/** |
|
|
23 |
* Form represents a form. |
|
|
24 |
* |
|
|
25 |
* A form is composed of a validator schema and a widget form schema. |
|
|
26 |
* |
|
|
27 |
* To implement your own form fields, you need to have a thorough understanding |
|
|
28 |
* of the data flow within a form field. A form field stores its data in three |
|
|
29 |
* different representations: |
|
|
30 |
* |
|
|
31 |
* (1) the format required by the form's object |
|
|
32 |
* (2) a normalized format for internal processing |
|
|
33 |
* (3) the format used for display |
|
|
34 |
* |
|
|
35 |
* A date field, for example, may store a date as "Y-m-d" string (1) in the |
|
|
36 |
* object. To facilitate processing in the field, this value is normalized |
|
|
37 |
* to a DateTime object (2). In the HTML representation of your form, a |
|
|
38 |
* localized string (3) is presented to and modified by the user. |
|
|
39 |
* |
|
|
40 |
* In most cases, format (1) and format (2) will be the same. For example, |
|
|
41 |
* a checkbox field uses a Boolean value both for internal processing as for |
|
|
42 |
* storage in the object. In these cases you simply need to set a value |
|
|
43 |
* transformer to convert between formats (2) and (3). You can do this by |
|
|
44 |
* calling appendClientTransformer(). |
|
|
45 |
* |
|
|
46 |
* In some cases though it makes sense to make format (1) configurable. To |
|
|
47 |
* demonstrate this, let's extend our above date field to store the value |
|
|
48 |
* either as "Y-m-d" string or as timestamp. Internally we still want to |
|
|
49 |
* use a DateTime object for processing. To convert the data from string/integer |
|
|
50 |
* to DateTime you can set a normalization transformer by calling |
|
|
51 |
* appendNormTransformer(). The normalized data is then |
|
|
52 |
* converted to the displayed data as described before. |
|
|
53 |
* |
|
|
54 |
* @author Fabien Potencier <fabien@symfony.com> |
|
|
55 |
* @author Bernhard Schussek <bernhard.schussek@symfony.com> |
|
|
56 |
*/ |
|
|
57 |
class Form implements \IteratorAggregate, FormInterface |
|
|
58 |
{ |
|
|
59 |
/** |
|
|
60 |
* The name of this form |
|
|
61 |
* @var string |
|
|
62 |
*/ |
|
|
63 |
private $name; |
|
|
64 |
|
|
|
65 |
/** |
|
|
66 |
* The parent of this form |
|
|
67 |
* @var FormInterface |
|
|
68 |
*/ |
|
|
69 |
private $parent; |
|
|
70 |
|
|
|
71 |
/** |
|
|
72 |
* The children of this form |
|
|
73 |
* @var array An array of FormInterface instances |
|
|
74 |
*/ |
|
|
75 |
private $children = array(); |
|
|
76 |
|
|
|
77 |
/** |
|
|
78 |
* The mapper for mapping data to children and back |
|
|
79 |
* @var DataMapper\DataMapperInterface |
|
|
80 |
*/ |
|
|
81 |
private $dataMapper; |
|
|
82 |
|
|
|
83 |
/** |
|
|
84 |
* The errors of this form |
|
|
85 |
* @var array An array of FromError instances |
|
|
86 |
*/ |
|
|
87 |
private $errors = array(); |
|
|
88 |
|
|
|
89 |
/** |
|
|
90 |
* Whether added errors should bubble up to the parent |
|
|
91 |
* @var Boolean |
|
|
92 |
*/ |
|
|
93 |
private $errorBubbling; |
|
|
94 |
|
|
|
95 |
/** |
|
|
96 |
* Whether this form is bound |
|
|
97 |
* @var Boolean |
|
|
98 |
*/ |
|
|
99 |
private $bound = false; |
|
|
100 |
|
|
|
101 |
/** |
|
|
102 |
* Whether this form may not be empty |
|
|
103 |
* @var Boolean |
|
|
104 |
*/ |
|
|
105 |
private $required; |
|
|
106 |
|
|
|
107 |
/** |
|
|
108 |
* The form data in application format |
|
|
109 |
* @var mixed |
|
|
110 |
*/ |
|
|
111 |
private $appData; |
|
|
112 |
|
|
|
113 |
/** |
|
|
114 |
* The form data in normalized format |
|
|
115 |
* @var mixed |
|
|
116 |
*/ |
|
|
117 |
private $normData; |
|
|
118 |
|
|
|
119 |
/** |
|
|
120 |
* The form data in client format |
|
|
121 |
* @var mixed |
|
|
122 |
*/ |
|
|
123 |
private $clientData; |
|
|
124 |
|
|
|
125 |
/** |
|
|
126 |
* Data used for the client data when no value is bound |
|
|
127 |
* @var mixed |
|
|
128 |
*/ |
|
|
129 |
private $emptyData = ''; |
|
|
130 |
|
|
|
131 |
/** |
|
|
132 |
* The bound values that don't belong to any children |
|
|
133 |
* @var array |
|
|
134 |
*/ |
|
|
135 |
private $extraData = array(); |
|
|
136 |
|
|
|
137 |
/** |
|
|
138 |
* The transformers for transforming from application to normalized format |
|
|
139 |
* and back |
|
|
140 |
* @var array An array of DataTransformerInterface |
|
|
141 |
*/ |
|
|
142 |
private $normTransformers; |
|
|
143 |
|
|
|
144 |
/** |
|
|
145 |
* The transformers for transforming from normalized to client format and |
|
|
146 |
* back |
|
|
147 |
* @var array An array of DataTransformerInterface |
|
|
148 |
*/ |
|
|
149 |
private $clientTransformers; |
|
|
150 |
|
|
|
151 |
/** |
|
|
152 |
* Whether the data in application, normalized and client format is |
|
|
153 |
* synchronized. Data may not be synchronized if transformation errors |
|
|
154 |
* occur. |
|
|
155 |
* @var Boolean |
|
|
156 |
*/ |
|
|
157 |
private $synchronized = true; |
|
|
158 |
|
|
|
159 |
/** |
|
|
160 |
* The validators attached to this form |
|
|
161 |
* @var array An array of FormValidatorInterface instances |
|
|
162 |
*/ |
|
|
163 |
private $validators; |
|
|
164 |
|
|
|
165 |
/** |
|
|
166 |
* Whether this form may only be read, but not bound |
|
|
167 |
* @var Boolean |
|
|
168 |
*/ |
|
|
169 |
private $readOnly = false; |
|
|
170 |
|
|
|
171 |
/** |
|
|
172 |
* The dispatcher for distributing events of this form |
|
|
173 |
* @var Symfony\Component\EventDispatcher\EventDispatcherInterface |
|
|
174 |
*/ |
|
|
175 |
private $dispatcher; |
|
|
176 |
|
|
|
177 |
/** |
|
|
178 |
* Key-value store for arbitrary attributes attached to this form |
|
|
179 |
* @var array |
|
|
180 |
*/ |
|
|
181 |
private $attributes; |
|
|
182 |
|
|
|
183 |
/** |
|
|
184 |
* The FormTypeInterface instances used to create this form |
|
|
185 |
* @var array An array of FormTypeInterface |
|
|
186 |
*/ |
|
|
187 |
private $types; |
|
|
188 |
|
|
|
189 |
public function __construct($name, EventDispatcherInterface $dispatcher, |
|
|
190 |
array $types = array(), array $clientTransformers = array(), |
|
|
191 |
array $normTransformers = array(), |
|
|
192 |
DataMapperInterface $dataMapper = null, array $validators = array(), |
|
|
193 |
$required = false, $readOnly = false, $errorBubbling = false, |
|
|
194 |
$emptyData = null, array $attributes = array()) |
|
|
195 |
{ |
|
|
196 |
foreach ($clientTransformers as $transformer) { |
|
|
197 |
if (!$transformer instanceof DataTransformerInterface) { |
|
|
198 |
throw new UnexpectedTypeException($transformer, 'Symfony\Component\Form\DataTransformerInterface'); |
|
|
199 |
} |
|
|
200 |
} |
|
|
201 |
|
|
|
202 |
foreach ($normTransformers as $transformer) { |
|
|
203 |
if (!$transformer instanceof DataTransformerInterface) { |
|
|
204 |
throw new UnexpectedTypeException($transformer, 'Symfony\Component\Form\DataTransformerInterface'); |
|
|
205 |
} |
|
|
206 |
} |
|
|
207 |
|
|
|
208 |
foreach ($validators as $validator) { |
|
|
209 |
if (!$validator instanceof FormValidatorInterface) { |
|
|
210 |
throw new UnexpectedTypeException($validator, 'Symfony\Component\Form\FormValidatorInterface'); |
|
|
211 |
} |
|
|
212 |
} |
|
|
213 |
|
|
|
214 |
$this->name = (string) $name; |
|
|
215 |
$this->dispatcher = $dispatcher; |
|
|
216 |
$this->types = $types; |
|
|
217 |
$this->clientTransformers = $clientTransformers; |
|
|
218 |
$this->normTransformers = $normTransformers; |
|
|
219 |
$this->dataMapper = $dataMapper; |
|
|
220 |
$this->validators = $validators; |
|
|
221 |
$this->required = (Boolean) $required; |
|
|
222 |
$this->readOnly = (Boolean) $readOnly; |
|
|
223 |
$this->errorBubbling = (Boolean) $errorBubbling; |
|
|
224 |
$this->emptyData = $emptyData; |
|
|
225 |
$this->attributes = $attributes; |
|
|
226 |
|
|
|
227 |
$this->setData(null); |
|
|
228 |
} |
|
|
229 |
|
|
|
230 |
public function __clone() |
|
|
231 |
{ |
|
|
232 |
foreach ($this->children as $key => $child) { |
|
|
233 |
$this->children[$key] = clone $child; |
|
|
234 |
} |
|
|
235 |
} |
|
|
236 |
|
|
|
237 |
/** |
|
|
238 |
* Returns the name by which the form is identified in forms. |
|
|
239 |
* |
|
|
240 |
* @return string The name of the form. |
|
|
241 |
*/ |
|
|
242 |
public function getName() |
|
|
243 |
{ |
|
|
244 |
return $this->name; |
|
|
245 |
} |
|
|
246 |
|
|
|
247 |
/** |
|
|
248 |
* Returns the types used by this form. |
|
|
249 |
* |
|
|
250 |
* @return array An array of FormTypeInterface |
|
|
251 |
*/ |
|
|
252 |
public function getTypes() |
|
|
253 |
{ |
|
|
254 |
return $this->types; |
|
|
255 |
} |
|
|
256 |
|
|
|
257 |
/** |
|
|
258 |
* Returns whether the form is required to be filled out. |
|
|
259 |
* |
|
|
260 |
* If the form has a parent and the parent is not required, this method |
|
|
261 |
* will always return false. Otherwise the value set with setRequired() |
|
|
262 |
* is returned. |
|
|
263 |
* |
|
|
264 |
* @return Boolean |
|
|
265 |
*/ |
|
|
266 |
public function isRequired() |
|
|
267 |
{ |
|
|
268 |
if (null === $this->parent || $this->parent->isRequired()) { |
|
|
269 |
|
|
|
270 |
return $this->required; |
|
|
271 |
} |
|
|
272 |
|
|
|
273 |
return false; |
|
|
274 |
} |
|
|
275 |
|
|
|
276 |
/** |
|
|
277 |
* Returns whether this form is read only. |
|
|
278 |
* |
|
|
279 |
* The content of a read-only form is displayed, but not allowed to be |
|
|
280 |
* modified. The validation of modified read-only forms should fail. |
|
|
281 |
* |
|
|
282 |
* Fields whose parents are read-only are considered read-only regardless of |
|
|
283 |
* their own state. |
|
|
284 |
* |
|
|
285 |
* @return Boolean |
|
|
286 |
*/ |
|
|
287 |
public function isReadOnly() |
|
|
288 |
{ |
|
|
289 |
if (null === $this->parent || !$this->parent->isReadOnly()) { |
|
|
290 |
|
|
|
291 |
return $this->readOnly; |
|
|
292 |
} |
|
|
293 |
|
|
|
294 |
return true; |
|
|
295 |
} |
|
|
296 |
|
|
|
297 |
/** |
|
|
298 |
* Sets the parent form. |
|
|
299 |
* |
|
|
300 |
* @param FormInterface $parent The parent form |
|
|
301 |
* |
|
|
302 |
* @return Form The current form |
|
|
303 |
*/ |
|
|
304 |
public function setParent(FormInterface $parent = null) |
|
|
305 |
{ |
|
|
306 |
$this->parent = $parent; |
|
|
307 |
|
|
|
308 |
return $this; |
|
|
309 |
} |
|
|
310 |
|
|
|
311 |
/** |
|
|
312 |
* Returns the parent field. |
|
|
313 |
* |
|
|
314 |
* @return FormInterface The parent field |
|
|
315 |
*/ |
|
|
316 |
public function getParent() |
|
|
317 |
{ |
|
|
318 |
return $this->parent; |
|
|
319 |
} |
|
|
320 |
|
|
|
321 |
/** |
|
|
322 |
* Returns whether the form has a parent. |
|
|
323 |
* |
|
|
324 |
* @return Boolean |
|
|
325 |
*/ |
|
|
326 |
public function hasParent() |
|
|
327 |
{ |
|
|
328 |
return null !== $this->parent; |
|
|
329 |
} |
|
|
330 |
|
|
|
331 |
/** |
|
|
332 |
* Returns the root of the form tree. |
|
|
333 |
* |
|
|
334 |
* @return FormInterface The root of the tree |
|
|
335 |
*/ |
|
|
336 |
public function getRoot() |
|
|
337 |
{ |
|
|
338 |
return $this->parent ? $this->parent->getRoot() : $this; |
|
|
339 |
} |
|
|
340 |
|
|
|
341 |
/** |
|
|
342 |
* Returns whether the field is the root of the form tree. |
|
|
343 |
* |
|
|
344 |
* @return Boolean |
|
|
345 |
*/ |
|
|
346 |
public function isRoot() |
|
|
347 |
{ |
|
|
348 |
return !$this->hasParent(); |
|
|
349 |
} |
|
|
350 |
|
|
|
351 |
/** |
|
|
352 |
* Returns whether the form has an attribute with the given name. |
|
|
353 |
* |
|
|
354 |
* @param string $name The name of the attribute |
|
|
355 |
* |
|
|
356 |
* @return Boolean |
|
|
357 |
*/ |
|
|
358 |
public function hasAttribute($name) |
|
|
359 |
{ |
|
|
360 |
return isset($this->attributes[$name]); |
|
|
361 |
} |
|
|
362 |
|
|
|
363 |
/** |
|
|
364 |
* Returns the value of the attributes with the given name. |
|
|
365 |
* |
|
|
366 |
* @param string $name The name of the attribute |
|
|
367 |
*/ |
|
|
368 |
public function getAttribute($name) |
|
|
369 |
{ |
|
|
370 |
return $this->attributes[$name]; |
|
|
371 |
} |
|
|
372 |
|
|
|
373 |
/** |
|
|
374 |
* Updates the field with default data. |
|
|
375 |
* |
|
|
376 |
* @param array $appData The data formatted as expected for the underlying object |
|
|
377 |
* |
|
|
378 |
* @return Form The current form |
|
|
379 |
*/ |
|
|
380 |
public function setData($appData) |
|
|
381 |
{ |
|
|
382 |
$event = new DataEvent($this, $appData); |
|
|
383 |
$this->dispatcher->dispatch(FormEvents::PRE_SET_DATA, $event); |
|
|
384 |
|
|
|
385 |
// Hook to change content of the data |
|
|
386 |
$event = new FilterDataEvent($this, $appData); |
|
|
387 |
$this->dispatcher->dispatch(FormEvents::SET_DATA, $event); |
|
|
388 |
$appData = $event->getData(); |
|
|
389 |
|
|
|
390 |
// Treat data as strings unless a value transformer exists |
|
|
391 |
if (!$this->clientTransformers && !$this->normTransformers && is_scalar($appData)) { |
|
|
392 |
$appData = (string) $appData; |
|
|
393 |
} |
|
|
394 |
|
|
|
395 |
// Synchronize representations - must not change the content! |
|
|
396 |
$normData = $this->appToNorm($appData); |
|
|
397 |
$clientData = $this->normToClient($normData); |
|
|
398 |
|
|
|
399 |
$this->appData = $appData; |
|
|
400 |
$this->normData = $normData; |
|
|
401 |
$this->clientData = $clientData; |
|
|
402 |
$this->synchronized = true; |
|
|
403 |
|
|
|
404 |
if ($this->dataMapper) { |
|
|
405 |
// Update child forms from the data |
|
|
406 |
$this->dataMapper->mapDataToForms($clientData, $this->children); |
|
|
407 |
} |
|
|
408 |
|
|
|
409 |
$event = new DataEvent($this, $appData); |
|
|
410 |
$this->dispatcher->dispatch(FormEvents::POST_SET_DATA, $event); |
|
|
411 |
|
|
|
412 |
return $this; |
|
|
413 |
} |
|
|
414 |
|
|
|
415 |
/** |
|
|
416 |
* Returns the data in the format needed for the underlying object. |
|
|
417 |
* |
|
|
418 |
* @return mixed |
|
|
419 |
*/ |
|
|
420 |
public function getData() |
|
|
421 |
{ |
|
|
422 |
return $this->appData; |
|
|
423 |
} |
|
|
424 |
|
|
|
425 |
/** |
|
|
426 |
* Returns the data transformed by the value transformer. |
|
|
427 |
* |
|
|
428 |
* @return string |
|
|
429 |
*/ |
|
|
430 |
public function getClientData() |
|
|
431 |
{ |
|
|
432 |
return $this->clientData; |
|
|
433 |
} |
|
|
434 |
|
|
|
435 |
/** |
|
|
436 |
* Returns the extra data. |
|
|
437 |
* |
|
|
438 |
* @return array The bound data which do not belong to a child |
|
|
439 |
*/ |
|
|
440 |
public function getExtraData() |
|
|
441 |
{ |
|
|
442 |
return $this->extraData; |
|
|
443 |
} |
|
|
444 |
|
|
|
445 |
/** |
|
|
446 |
* Binds data to the field, transforms and validates it. |
|
|
447 |
* |
|
|
448 |
* @param string|array $clientData The data |
|
|
449 |
* |
|
|
450 |
* @return Form The current form |
|
|
451 |
* |
|
|
452 |
* @throws UnexpectedTypeException |
|
|
453 |
*/ |
|
|
454 |
public function bind($clientData) |
|
|
455 |
{ |
|
|
456 |
if ($this->readOnly) { |
|
|
457 |
$this->bound = true; |
|
|
458 |
|
|
|
459 |
return $this; |
|
|
460 |
} |
|
|
461 |
|
|
|
462 |
if (is_scalar($clientData) || null === $clientData) { |
|
|
463 |
$clientData = (string) $clientData; |
|
|
464 |
} |
|
|
465 |
|
|
|
466 |
// Initialize errors in the very beginning so that we don't lose any |
|
|
467 |
// errors added during listeners |
|
|
468 |
$this->errors = array(); |
|
|
469 |
|
|
|
470 |
$event = new DataEvent($this, $clientData); |
|
|
471 |
$this->dispatcher->dispatch(FormEvents::PRE_BIND, $event); |
|
|
472 |
|
|
|
473 |
$appData = null; |
|
|
474 |
$normData = null; |
|
|
475 |
$extraData = array(); |
|
|
476 |
$synchronized = false; |
|
|
477 |
|
|
|
478 |
// Hook to change content of the data bound by the browser |
|
|
479 |
$event = new FilterDataEvent($this, $clientData); |
|
|
480 |
$this->dispatcher->dispatch(FormEvents::BIND_CLIENT_DATA, $event); |
|
|
481 |
$clientData = $event->getData(); |
|
|
482 |
|
|
|
483 |
if (count($this->children) > 0) { |
|
|
484 |
if (null === $clientData || '' === $clientData) { |
|
|
485 |
$clientData = array(); |
|
|
486 |
} |
|
|
487 |
|
|
|
488 |
if (!is_array($clientData)) { |
|
|
489 |
throw new UnexpectedTypeException($clientData, 'array'); |
|
|
490 |
} |
|
|
491 |
|
|
|
492 |
foreach ($this->children as $name => $child) { |
|
|
493 |
if (!isset($clientData[$name])) { |
|
|
494 |
$clientData[$name] = null; |
|
|
495 |
} |
|
|
496 |
} |
|
|
497 |
|
|
|
498 |
foreach ($clientData as $name => $value) { |
|
|
499 |
if ($this->has($name)) { |
|
|
500 |
$this->children[$name]->bind($value); |
|
|
501 |
} else { |
|
|
502 |
$extraData[$name] = $value; |
|
|
503 |
} |
|
|
504 |
} |
|
|
505 |
|
|
|
506 |
// If we have a data mapper, use old client data and merge |
|
|
507 |
// data from the children into it later |
|
|
508 |
if ($this->dataMapper) { |
|
|
509 |
$clientData = $this->getClientData(); |
|
|
510 |
} |
|
|
511 |
} |
|
|
512 |
|
|
|
513 |
if (null === $clientData || '' === $clientData) { |
|
|
514 |
$clientData = $this->emptyData; |
|
|
515 |
|
|
|
516 |
if ($clientData instanceof \Closure) { |
|
|
517 |
$clientData = $clientData($this); |
|
|
518 |
} |
|
|
519 |
} |
|
|
520 |
|
|
|
521 |
// Merge form data from children into existing client data |
|
|
522 |
if (count($this->children) > 0 && $this->dataMapper) { |
|
|
523 |
$this->dataMapper->mapFormsToData($this->children, $clientData); |
|
|
524 |
} |
|
|
525 |
|
|
|
526 |
try { |
|
|
527 |
// Normalize data to unified representation |
|
|
528 |
$normData = $this->clientToNorm($clientData); |
|
|
529 |
$synchronized = true; |
|
|
530 |
} catch (TransformationFailedException $e) { |
|
|
531 |
} |
|
|
532 |
|
|
|
533 |
if ($synchronized) { |
|
|
534 |
// Hook to change content of the data in the normalized |
|
|
535 |
// representation |
|
|
536 |
$event = new FilterDataEvent($this, $normData); |
|
|
537 |
$this->dispatcher->dispatch(FormEvents::BIND_NORM_DATA, $event); |
|
|
538 |
$normData = $event->getData(); |
|
|
539 |
|
|
|
540 |
// Synchronize representations - must not change the content! |
|
|
541 |
$appData = $this->normToApp($normData); |
|
|
542 |
$clientData = $this->normToClient($normData); |
|
|
543 |
} |
|
|
544 |
|
|
|
545 |
$this->bound = true; |
|
|
546 |
$this->appData = $appData; |
|
|
547 |
$this->normData = $normData; |
|
|
548 |
$this->clientData = $clientData; |
|
|
549 |
$this->extraData = $extraData; |
|
|
550 |
$this->synchronized = $synchronized; |
|
|
551 |
|
|
|
552 |
$event = new DataEvent($this, $clientData); |
|
|
553 |
$this->dispatcher->dispatch(FormEvents::POST_BIND, $event); |
|
|
554 |
|
|
|
555 |
foreach ($this->validators as $validator) { |
|
|
556 |
$validator->validate($this); |
|
|
557 |
} |
|
|
558 |
|
|
|
559 |
return $this; |
|
|
560 |
} |
|
|
561 |
|
|
|
562 |
/** |
|
|
563 |
* Binds a request to the form. |
|
|
564 |
* |
|
|
565 |
* If the request method is POST, PUT or GET, the data is bound to the form, |
|
|
566 |
* transformed and written into the form data (an object or an array). |
|
|
567 |
* |
|
|
568 |
* @param Request $request The request to bind to the form |
|
|
569 |
* |
|
|
570 |
* @return Form This form |
|
|
571 |
* |
|
|
572 |
* @throws FormException if the method of the request is not one of GET, POST or PUT |
|
|
573 |
*/ |
|
|
574 |
public function bindRequest(Request $request) |
|
|
575 |
{ |
|
|
576 |
// Store the bound data in case of a post request |
|
|
577 |
switch ($request->getMethod()) { |
|
|
578 |
case 'POST': |
|
|
579 |
case 'PUT': |
|
|
580 |
$data = array_replace_recursive( |
|
|
581 |
$request->request->get($this->getName(), array()), |
|
|
582 |
$request->files->get($this->getName(), array()) |
|
|
583 |
); |
|
|
584 |
break; |
|
|
585 |
case 'GET': |
|
|
586 |
$data = $request->query->get($this->getName(), array()); |
|
|
587 |
break; |
|
|
588 |
default: |
|
|
589 |
throw new FormException(sprintf('The request method "%s" is not supported', $request->getMethod())); |
|
|
590 |
} |
|
|
591 |
|
|
|
592 |
return $this->bind($data); |
|
|
593 |
} |
|
|
594 |
|
|
|
595 |
/** |
|
|
596 |
* Returns the normalized data of the field. |
|
|
597 |
* |
|
|
598 |
* @return mixed When the field is not bound, the default data is returned. |
|
|
599 |
* When the field is bound, the normalized bound data is |
|
|
600 |
* returned if the field is valid, null otherwise. |
|
|
601 |
*/ |
|
|
602 |
public function getNormData() |
|
|
603 |
{ |
|
|
604 |
return $this->normData; |
|
|
605 |
} |
|
|
606 |
|
|
|
607 |
/** |
|
|
608 |
* Adds an error to this form. |
|
|
609 |
* |
|
|
610 |
* @param FormError $error |
|
|
611 |
* |
|
|
612 |
* @return Form The current form |
|
|
613 |
*/ |
|
|
614 |
public function addError(FormError $error) |
|
|
615 |
{ |
|
|
616 |
if ($this->parent && $this->errorBubbling) { |
|
|
617 |
$this->parent->addError($error); |
|
|
618 |
} else { |
|
|
619 |
$this->errors[] = $error; |
|
|
620 |
} |
|
|
621 |
|
|
|
622 |
return $this; |
|
|
623 |
} |
|
|
624 |
|
|
|
625 |
/** |
|
|
626 |
* Returns whether errors bubble up to the parent. |
|
|
627 |
* |
|
|
628 |
* @return Boolean |
|
|
629 |
*/ |
|
|
630 |
public function getErrorBubbling() |
|
|
631 |
{ |
|
|
632 |
return $this->errorBubbling; |
|
|
633 |
} |
|
|
634 |
|
|
|
635 |
/** |
|
|
636 |
* Returns whether the field is bound. |
|
|
637 |
* |
|
|
638 |
* @return Boolean true if the form is bound to input values, false otherwise |
|
|
639 |
*/ |
|
|
640 |
public function isBound() |
|
|
641 |
{ |
|
|
642 |
return $this->bound; |
|
|
643 |
} |
|
|
644 |
|
|
|
645 |
/** |
|
|
646 |
* Returns whether the data in the different formats is synchronized. |
|
|
647 |
* |
|
|
648 |
* @return Boolean |
|
|
649 |
*/ |
|
|
650 |
public function isSynchronized() |
|
|
651 |
{ |
|
|
652 |
return $this->synchronized; |
|
|
653 |
} |
|
|
654 |
|
|
|
655 |
/** |
|
|
656 |
* Returns whether the form is empty. |
|
|
657 |
* |
|
|
658 |
* @return Boolean |
|
|
659 |
*/ |
|
|
660 |
public function isEmpty() |
|
|
661 |
{ |
|
|
662 |
foreach ($this->children as $child) { |
|
|
663 |
if (!$child->isEmpty()) { |
|
|
664 |
|
|
|
665 |
return false; |
|
|
666 |
} |
|
|
667 |
} |
|
|
668 |
|
|
|
669 |
return array() === $this->appData || null === $this->appData || '' === $this->appData; |
|
|
670 |
} |
|
|
671 |
|
|
|
672 |
/** |
|
|
673 |
* Returns whether the field is valid. |
|
|
674 |
* |
|
|
675 |
* @return Boolean |
|
|
676 |
*/ |
|
|
677 |
public function isValid() |
|
|
678 |
{ |
|
|
679 |
if (!$this->isBound()) { |
|
|
680 |
throw new \LogicException('You cannot call isValid() on a form that is not bound.'); |
|
|
681 |
} |
|
|
682 |
|
|
|
683 |
if ($this->hasErrors()) { |
|
|
684 |
return false; |
|
|
685 |
} |
|
|
686 |
|
|
|
687 |
if (!$this->readOnly) { |
|
|
688 |
foreach ($this->children as $child) { |
|
|
689 |
if (!$child->isValid()) { |
|
|
690 |
|
|
|
691 |
return false; |
|
|
692 |
} |
|
|
693 |
} |
|
|
694 |
} |
|
|
695 |
|
|
|
696 |
return true; |
|
|
697 |
} |
|
|
698 |
|
|
|
699 |
/** |
|
|
700 |
* Returns whether or not there are errors. |
|
|
701 |
* |
|
|
702 |
* @return Boolean true if form is bound and not valid |
|
|
703 |
*/ |
|
|
704 |
public function hasErrors() |
|
|
705 |
{ |
|
|
706 |
// Don't call isValid() here, as its semantics are slightly different |
|
|
707 |
// Field groups are not valid if their children are invalid, but |
|
|
708 |
// hasErrors() returns only true if a field/field group itself has |
|
|
709 |
// errors |
|
|
710 |
return count($this->errors) > 0; |
|
|
711 |
} |
|
|
712 |
|
|
|
713 |
/** |
|
|
714 |
* Returns all errors. |
|
|
715 |
* |
|
|
716 |
* @return array An array of FormError instances that occurred during binding |
|
|
717 |
*/ |
|
|
718 |
public function getErrors() |
|
|
719 |
{ |
|
|
720 |
return $this->errors; |
|
|
721 |
} |
|
|
722 |
|
|
|
723 |
/** |
|
|
724 |
* Returns the DataTransformers. |
|
|
725 |
* |
|
|
726 |
* @return array An array of DataTransformerInterface |
|
|
727 |
*/ |
|
|
728 |
public function getNormTransformers() |
|
|
729 |
{ |
|
|
730 |
return $this->normTransformers; |
|
|
731 |
} |
|
|
732 |
|
|
|
733 |
/** |
|
|
734 |
* Returns the DataTransformers. |
|
|
735 |
* |
|
|
736 |
* @return array An array of DataTransformerInterface |
|
|
737 |
*/ |
|
|
738 |
public function getClientTransformers() |
|
|
739 |
{ |
|
|
740 |
return $this->clientTransformers; |
|
|
741 |
} |
|
|
742 |
|
|
|
743 |
/** |
|
|
744 |
* Returns all children in this group. |
|
|
745 |
* |
|
|
746 |
* @return array |
|
|
747 |
*/ |
|
|
748 |
public function getChildren() |
|
|
749 |
{ |
|
|
750 |
return $this->children; |
|
|
751 |
} |
|
|
752 |
|
|
|
753 |
/** |
|
|
754 |
* Return whether the form has children. |
|
|
755 |
* |
|
|
756 |
* @return Boolean |
|
|
757 |
*/ |
|
|
758 |
public function hasChildren() |
|
|
759 |
{ |
|
|
760 |
return count($this->children) > 0; |
|
|
761 |
} |
|
|
762 |
|
|
|
763 |
/** |
|
|
764 |
* Adds a child to the form. |
|
|
765 |
* |
|
|
766 |
* @param FormInterface $child The FormInterface to add as a child |
|
|
767 |
* |
|
|
768 |
* @return Form the current form |
|
|
769 |
*/ |
|
|
770 |
public function add(FormInterface $child) |
|
|
771 |
{ |
|
|
772 |
$this->children[$child->getName()] = $child; |
|
|
773 |
|
|
|
774 |
$child->setParent($this); |
|
|
775 |
|
|
|
776 |
if ($this->dataMapper) { |
|
|
777 |
$this->dataMapper->mapDataToForm($this->getClientData(), $child); |
|
|
778 |
} |
|
|
779 |
|
|
|
780 |
return $this; |
|
|
781 |
} |
|
|
782 |
|
|
|
783 |
/** |
|
|
784 |
* Removes a child from the form. |
|
|
785 |
* |
|
|
786 |
* @param string $name The name of the child to remove |
|
|
787 |
* |
|
|
788 |
* @return Form the current form |
|
|
789 |
*/ |
|
|
790 |
public function remove($name) |
|
|
791 |
{ |
|
|
792 |
if (isset($this->children[$name])) { |
|
|
793 |
$this->children[$name]->setParent(null); |
|
|
794 |
|
|
|
795 |
unset($this->children[$name]); |
|
|
796 |
} |
|
|
797 |
|
|
|
798 |
return $this; |
|
|
799 |
} |
|
|
800 |
|
|
|
801 |
/** |
|
|
802 |
* Returns whether a child with the given name exists. |
|
|
803 |
* |
|
|
804 |
* @param string $name |
|
|
805 |
* |
|
|
806 |
* @return Boolean |
|
|
807 |
*/ |
|
|
808 |
public function has($name) |
|
|
809 |
{ |
|
|
810 |
return isset($this->children[$name]); |
|
|
811 |
} |
|
|
812 |
|
|
|
813 |
/** |
|
|
814 |
* Returns the child with the given name. |
|
|
815 |
* |
|
|
816 |
* @param string $name |
|
|
817 |
* |
|
|
818 |
* @return FormInterface |
|
|
819 |
* |
|
|
820 |
* @throws \InvalidArgumentException if the child does not exist |
|
|
821 |
*/ |
|
|
822 |
public function get($name) |
|
|
823 |
{ |
|
|
824 |
if (isset($this->children[$name])) { |
|
|
825 |
|
|
|
826 |
return $this->children[$name]; |
|
|
827 |
} |
|
|
828 |
|
|
|
829 |
throw new \InvalidArgumentException(sprintf('Field "%s" does not exist.', $name)); |
|
|
830 |
} |
|
|
831 |
|
|
|
832 |
/** |
|
|
833 |
* Returns true if the child exists (implements the \ArrayAccess interface). |
|
|
834 |
* |
|
|
835 |
* @param string $name The name of the child |
|
|
836 |
* |
|
|
837 |
* @return Boolean true if the widget exists, false otherwise |
|
|
838 |
*/ |
|
|
839 |
public function offsetExists($name) |
|
|
840 |
{ |
|
|
841 |
return $this->has($name); |
|
|
842 |
} |
|
|
843 |
|
|
|
844 |
/** |
|
|
845 |
* Returns the form child associated with the name (implements the \ArrayAccess interface). |
|
|
846 |
* |
|
|
847 |
* @param string $name The offset of the value to get |
|
|
848 |
* |
|
|
849 |
* @return FormInterface A form instance |
|
|
850 |
*/ |
|
|
851 |
public function offsetGet($name) |
|
|
852 |
{ |
|
|
853 |
return $this->get($name); |
|
|
854 |
} |
|
|
855 |
|
|
|
856 |
/** |
|
|
857 |
* Adds a child to the form (implements the \ArrayAccess interface). |
|
|
858 |
* |
|
|
859 |
* @param string $name Ignored. The name of the child is used. |
|
|
860 |
* @param FormInterface $child The child to be added |
|
|
861 |
*/ |
|
|
862 |
public function offsetSet($name, $child) |
|
|
863 |
{ |
|
|
864 |
$this->add($child); |
|
|
865 |
} |
|
|
866 |
|
|
|
867 |
/** |
|
|
868 |
* Removes the child with the given name from the form (implements the \ArrayAccess interface). |
|
|
869 |
* |
|
|
870 |
* @param string $name The name of the child to be removed |
|
|
871 |
*/ |
|
|
872 |
public function offsetUnset($name) |
|
|
873 |
{ |
|
|
874 |
$this->remove($name); |
|
|
875 |
} |
|
|
876 |
|
|
|
877 |
/** |
|
|
878 |
* Returns the iterator for this group. |
|
|
879 |
* |
|
|
880 |
* @return \ArrayIterator |
|
|
881 |
*/ |
|
|
882 |
public function getIterator() |
|
|
883 |
{ |
|
|
884 |
return new \ArrayIterator($this->children); |
|
|
885 |
} |
|
|
886 |
|
|
|
887 |
/** |
|
|
888 |
* Returns the number of form children (implements the \Countable interface). |
|
|
889 |
* |
|
|
890 |
* @return integer The number of embedded form children |
|
|
891 |
*/ |
|
|
892 |
public function count() |
|
|
893 |
{ |
|
|
894 |
return count($this->children); |
|
|
895 |
} |
|
|
896 |
|
|
|
897 |
/** |
|
|
898 |
* Creates a view. |
|
|
899 |
* |
|
|
900 |
* @param FormView $parent The parent view |
|
|
901 |
* |
|
|
902 |
* @return FormView The view |
|
|
903 |
*/ |
|
|
904 |
public function createView(FormView $parent = null) |
|
|
905 |
{ |
|
|
906 |
if (null === $parent && $this->parent) { |
|
|
907 |
$parent = $this->parent->createView(); |
|
|
908 |
} |
|
|
909 |
|
|
|
910 |
$view = new FormView(); |
|
|
911 |
|
|
|
912 |
$view->setParent($parent); |
|
|
913 |
|
|
|
914 |
$types = (array) $this->types; |
|
|
915 |
|
|
|
916 |
foreach ($types as $type) { |
|
|
917 |
$type->buildView($view, $this); |
|
|
918 |
|
|
|
919 |
foreach ($type->getExtensions() as $typeExtension) { |
|
|
920 |
$typeExtension->buildView($view, $this); |
|
|
921 |
} |
|
|
922 |
} |
|
|
923 |
|
|
|
924 |
$childViews = array(); |
|
|
925 |
|
|
|
926 |
foreach ($this->children as $key => $child) { |
|
|
927 |
$childViews[$key] = $child->createView($view); |
|
|
928 |
} |
|
|
929 |
|
|
|
930 |
$view->setChildren($childViews); |
|
|
931 |
|
|
|
932 |
foreach ($types as $type) { |
|
|
933 |
$type->buildViewBottomUp($view, $this); |
|
|
934 |
|
|
|
935 |
foreach ($type->getExtensions() as $typeExtension) { |
|
|
936 |
$typeExtension->buildViewBottomUp($view, $this); |
|
|
937 |
} |
|
|
938 |
} |
|
|
939 |
|
|
|
940 |
return $view; |
|
|
941 |
} |
|
|
942 |
|
|
|
943 |
/** |
|
|
944 |
* Normalizes the value if a normalization transformer is set. |
|
|
945 |
* |
|
|
946 |
* @param mixed $value The value to transform |
|
|
947 |
* |
|
|
948 |
* @return string |
|
|
949 |
*/ |
|
|
950 |
private function appToNorm($value) |
|
|
951 |
{ |
|
|
952 |
foreach ($this->normTransformers as $transformer) { |
|
|
953 |
$value = $transformer->transform($value); |
|
|
954 |
} |
|
|
955 |
|
|
|
956 |
return $value; |
|
|
957 |
} |
|
|
958 |
|
|
|
959 |
/** |
|
|
960 |
* Reverse transforms a value if a normalization transformer is set. |
|
|
961 |
* |
|
|
962 |
* @param string $value The value to reverse transform |
|
|
963 |
* |
|
|
964 |
* @return mixed |
|
|
965 |
*/ |
|
|
966 |
private function normToApp($value) |
|
|
967 |
{ |
|
|
968 |
for ($i = count($this->normTransformers) - 1; $i >= 0; --$i) { |
|
|
969 |
$value = $this->normTransformers[$i]->reverseTransform($value); |
|
|
970 |
} |
|
|
971 |
|
|
|
972 |
return $value; |
|
|
973 |
} |
|
|
974 |
|
|
|
975 |
/** |
|
|
976 |
* Transforms the value if a value transformer is set. |
|
|
977 |
* |
|
|
978 |
* @param mixed $value The value to transform |
|
|
979 |
* |
|
|
980 |
* @return string |
|
|
981 |
*/ |
|
|
982 |
private function normToClient($value) |
|
|
983 |
{ |
|
|
984 |
if (!$this->clientTransformers) { |
|
|
985 |
// Scalar values should always be converted to strings to |
|
|
986 |
// facilitate differentiation between empty ("") and zero (0). |
|
|
987 |
return null === $value || is_scalar($value) ? (string) $value : $value; |
|
|
988 |
} |
|
|
989 |
|
|
|
990 |
foreach ($this->clientTransformers as $transformer) { |
|
|
991 |
$value = $transformer->transform($value); |
|
|
992 |
} |
|
|
993 |
|
|
|
994 |
return $value; |
|
|
995 |
} |
|
|
996 |
|
|
|
997 |
/** |
|
|
998 |
* Reverse transforms a value if a value transformer is set. |
|
|
999 |
* |
|
|
1000 |
* @param string $value The value to reverse transform |
|
|
1001 |
* |
|
|
1002 |
* @return mixed |
|
|
1003 |
*/ |
|
|
1004 |
private function clientToNorm($value) |
|
|
1005 |
{ |
|
|
1006 |
if (!$this->clientTransformers) { |
|
|
1007 |
return '' === $value ? null : $value; |
|
|
1008 |
} |
|
|
1009 |
|
|
|
1010 |
for ($i = count($this->clientTransformers) - 1; $i >= 0; --$i) { |
|
|
1011 |
$value = $this->clientTransformers[$i]->reverseTransform($value); |
|
|
1012 |
} |
|
|
1013 |
|
|
|
1014 |
return $value; |
|
|
1015 |
} |
|
|
1016 |
} |