Form.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Form;
  11. use Symfony\Component\HttpFoundation\Request;
  12. use Symfony\Component\HttpFoundation\FileBag;
  13. use Symfony\Component\Validator\ExecutionContext;
  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\MissingOptionsException;
  18. use Symfony\Component\Form\Exception\AlreadyBoundException;
  19. use Symfony\Component\Form\Exception\UnexpectedTypeException;
  20. use Symfony\Component\Form\Exception\DanglingFieldException;
  21. use Symfony\Component\Form\Exception\FieldDefinitionException;
  22. use Symfony\Component\Form\CsrfProvider\CsrfProviderInterface;
  23. use Symfony\Component\Form\DataTransformer\DataTransformerInterface;
  24. use Symfony\Component\Form\DataTransformer\TransformationFailedException;
  25. use Symfony\Component\Form\DataMapper\DataMapperInterface;
  26. use Symfony\Component\Form\Validator\FormValidatorInterface;
  27. use Symfony\Component\Form\Renderer\FormRendererInterface;
  28. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  29. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  30. /**
  31. * Form represents a form.
  32. *
  33. * A form is composed of a validator schema and a widget form schema.
  34. *
  35. * Form also takes care of CSRF protection by default.
  36. *
  37. * A CSRF secret can be any random string. If set to false, it disables the
  38. * CSRF protection, and if set to null, it forces the form to use the global
  39. * CSRF secret. If the global CSRF secret is also null, then a random one
  40. * is generated on the fly.
  41. *
  42. * To implement your own form fields, you need to have a thorough understanding
  43. * of the data flow within a form field. A form field stores its data in three
  44. * different representations:
  45. *
  46. * (1) the format required by the form's object
  47. * (2) a normalized format for internal processing
  48. * (3) the format used for display
  49. *
  50. * A date field, for example, may store a date as "Y-m-d" string (1) in the
  51. * object. To facilitate processing in the field, this value is normalized
  52. * to a DateTime object (2). In the HTML representation of your form, a
  53. * localized string (3) is presented to and modified by the user.
  54. *
  55. * In most cases, format (1) and format (2) will be the same. For example,
  56. * a checkbox field uses a Boolean value both for internal processing as for
  57. * storage in the object. In these cases you simply need to set a value
  58. * transformer to convert between formats (2) and (3). You can do this by
  59. * calling setClientTransformer() in the configure() method.
  60. *
  61. * In some cases though it makes sense to make format (1) configurable. To
  62. * demonstrate this, let's extend our above date field to store the value
  63. * either as "Y-m-d" string or as timestamp. Internally we still want to
  64. * use a DateTime object for processing. To convert the data from string/integer
  65. * to DateTime you can set a normalization transformer by calling
  66. * setNormTransformer() in configure(). The normalized data is then
  67. * converted to the displayed data as described before.
  68. *
  69. * @author Fabien Potencier <fabien@symfony.com>
  70. * @author Bernhard Schussek <bernhard.schussek@symfony.com>
  71. */
  72. class Form implements \IteratorAggregate, FormInterface
  73. {
  74. /**
  75. * Contains all the children of this group
  76. * @var array
  77. */
  78. private $children = array();
  79. private $dataMapper;
  80. private $errors = array();
  81. private $errorBubbling;
  82. private $name = '';
  83. private $parent;
  84. private $bound = false;
  85. private $required;
  86. private $data;
  87. private $normData;
  88. private $clientData;
  89. /**
  90. * Contains the names of bound values who don't belong to any children
  91. * @var array
  92. */
  93. private $extraData = array();
  94. private $normTransformer;
  95. private $clientTransformer;
  96. private $synchronized = true;
  97. private $validators;
  98. private $renderer;
  99. private $readOnly = false;
  100. private $dispatcher;
  101. private $attributes;
  102. public function __construct($name, EventDispatcherInterface $dispatcher,
  103. FormRendererInterface $renderer = null, DataTransformerInterface $clientTransformer = null,
  104. DataTransformerInterface $normTransformer = null,
  105. DataMapperInterface $dataMapper = null, array $validators = array(),
  106. $required = false, $readOnly = false, $errorBubbling = false,
  107. array $attributes = array())
  108. {
  109. foreach ($validators as $validator) {
  110. if (!$validator instanceof FormValidatorInterface) {
  111. throw new UnexpectedTypeException($validator, 'Symfony\Component\Form\Validator\FormValidatorInterface');
  112. }
  113. }
  114. $this->name = (string)$name;
  115. $this->dispatcher = $dispatcher;
  116. $this->renderer = $renderer;
  117. $this->clientTransformer = $clientTransformer;
  118. $this->normTransformer = $normTransformer;
  119. $this->validators = $validators;
  120. $this->dataMapper = $dataMapper;
  121. $this->required = $required;
  122. $this->readOnly = $readOnly;
  123. $this->attributes = $attributes;
  124. $this->errorBubbling = $errorBubbling;
  125. if ($renderer) {
  126. $renderer->setForm($this);
  127. }
  128. $this->setData(null);
  129. }
  130. public function __clone()
  131. {
  132. foreach ($this->children as $key => $child) {
  133. $this->children[$key] = clone $child;
  134. }
  135. }
  136. /**
  137. * {@inheritDoc}
  138. */
  139. public function getName()
  140. {
  141. return $this->name;
  142. }
  143. /**
  144. * {@inheritDoc}
  145. */
  146. public function isRequired()
  147. {
  148. if (null === $this->parent || $this->parent->isRequired()) {
  149. return $this->required;
  150. }
  151. return false;
  152. }
  153. /**
  154. * {@inheritDoc}
  155. */
  156. public function isReadOnly()
  157. {
  158. if (null === $this->parent || !$this->parent->isReadOnly()) {
  159. return $this->readOnly;
  160. }
  161. return true;
  162. }
  163. /**
  164. * {@inheritDoc}
  165. */
  166. public function setParent(FormInterface $parent = null)
  167. {
  168. $this->parent = $parent;
  169. return $this;
  170. }
  171. /**
  172. * Returns the parent field.
  173. *
  174. * @return FormInterface The parent field
  175. */
  176. public function getParent()
  177. {
  178. return $this->parent;
  179. }
  180. /**
  181. * Returns whether the field has a parent.
  182. *
  183. * @return Boolean
  184. */
  185. public function hasParent()
  186. {
  187. return null !== $this->parent;
  188. }
  189. /**
  190. * Returns the root of the form tree
  191. *
  192. * @return FormInterface The root of the tree
  193. */
  194. public function getRoot()
  195. {
  196. return $this->parent ? $this->parent->getRoot() : $this;
  197. }
  198. /**
  199. * Returns whether the field is the root of the form tree
  200. *
  201. * @return Boolean
  202. */
  203. public function isRoot()
  204. {
  205. return !$this->hasParent();
  206. }
  207. public function hasAttribute($name)
  208. {
  209. return isset($this->attributes[$name]);
  210. }
  211. public function getAttribute($name)
  212. {
  213. return $this->attributes[$name];
  214. }
  215. /**
  216. * Updates the field with default data
  217. *
  218. * @see FormInterface
  219. */
  220. public function setData($appData)
  221. {
  222. $event = new DataEvent($this, $appData);
  223. $this->dispatcher->dispatch(Events::preSetData, $event);
  224. // Hook to change content of the data
  225. $event = new FilterDataEvent($this, $appData);
  226. $this->dispatcher->dispatch(Events::filterSetData, $event);
  227. $appData = $event->getData();
  228. // Fix data if empty
  229. if (!$this->clientTransformer) {
  230. if (empty($appData) && !$this->normTransformer && $this->dataMapper) {
  231. $appData = $this->dataMapper->createEmptyData();
  232. }
  233. // Treat data as strings unless a value transformer exists
  234. if (is_scalar($appData)) {
  235. $appData = (string)$appData;
  236. }
  237. }
  238. // Synchronize representations - must not change the content!
  239. $normData = $this->appToNorm($appData);
  240. $clientData = $this->normToClient($normData);
  241. $this->data = $appData;
  242. $this->normData = $normData;
  243. $this->clientData = $clientData;
  244. $this->synchronized = true;
  245. if ($this->dataMapper) {
  246. // Update child forms from the data
  247. $this->dataMapper->mapDataToForms($clientData, $this->children);
  248. }
  249. $event = new DataEvent($this, $appData);
  250. $this->dispatcher->dispatch(Events::postSetData, $event);
  251. return $this;
  252. }
  253. /**
  254. * Binds POST data to the field, transforms and validates it.
  255. *
  256. * @param string|array $data The POST data
  257. */
  258. public function bind($clientData)
  259. {
  260. if (is_scalar($clientData) || null === $clientData) {
  261. $clientData = (string)$clientData;
  262. }
  263. $event = new DataEvent($this, $clientData);
  264. $this->dispatcher->dispatch(Events::preBind, $event);
  265. $appData = null;
  266. $normData = null;
  267. $extraData = array();
  268. $synchronized = false;
  269. // Hook to change content of the data bound by the browser
  270. $event = new FilterDataEvent($this, $clientData);
  271. $this->dispatcher->dispatch(Events::filterBoundClientData, $event);
  272. $clientData = $event->getData();
  273. if (count($this->children) > 0) {
  274. if (empty($clientData)) {
  275. $clientData = array();
  276. }
  277. if (!is_array($clientData)) {
  278. throw new UnexpectedTypeException($clientData, 'array');
  279. }
  280. foreach ($this->children as $name => $child) {
  281. if (!isset($clientData[$name])) {
  282. $clientData[$name] = null;
  283. }
  284. }
  285. foreach ($clientData as $name => $value) {
  286. if ($this->has($name)) {
  287. $this->children[$name]->bind($value);
  288. } else {
  289. $extraData[$name] = $value;
  290. }
  291. }
  292. // Merge form data from children into existing client data
  293. if ($this->dataMapper) {
  294. $clientData = $this->getClientData();
  295. $this->dataMapper->mapFormsToData($this->children, $clientData);
  296. }
  297. }
  298. try {
  299. // Normalize data to unified representation
  300. $normData = $this->clientToNorm($clientData);
  301. $synchronized = true;
  302. } catch (TransformationFailedException $e) {
  303. }
  304. if ($synchronized) {
  305. // Hook to change content of the data in the normalized
  306. // representation
  307. $event = new FilterDataEvent($this, $normData);
  308. $this->dispatcher->dispatch(Events::filterBoundNormData, $event);
  309. $normData = $event->getData();
  310. // Synchronize representations - must not change the content!
  311. $appData = $this->normToApp($normData);
  312. $clientData = $this->normToClient($normData);
  313. }
  314. $this->bound = true;
  315. $this->errors = array();
  316. $this->data = $appData;
  317. $this->normData = $normData;
  318. $this->clientData = $clientData;
  319. $this->extraData = $extraData;
  320. $this->synchronized = $synchronized;
  321. $event = new DataEvent($this, $clientData);
  322. $this->dispatcher->dispatch(Events::postBind, $event);
  323. foreach ($this->validators as $validator) {
  324. $validator->validate($this);
  325. }
  326. }
  327. /**
  328. * Returns the data in the format needed for the underlying object.
  329. *
  330. * @return mixed
  331. */
  332. public function getData()
  333. {
  334. return $this->data;
  335. }
  336. /**
  337. * Returns the normalized data of the field.
  338. *
  339. * @return mixed When the field is not bound, the default data is returned.
  340. * When the field is bound, the normalized bound data is
  341. * returned if the field is valid, null otherwise.
  342. */
  343. public function getNormData()
  344. {
  345. return $this->normData;
  346. }
  347. /**
  348. * Returns the data transformed by the value transformer
  349. *
  350. * @return string
  351. */
  352. public function getClientData()
  353. {
  354. return $this->clientData;
  355. }
  356. public function getExtraData()
  357. {
  358. return $this->extraData;
  359. }
  360. /**
  361. * Adds an error to the field.
  362. *
  363. * @see FormInterface
  364. */
  365. public function addError(FormError $error)
  366. {
  367. if ($this->parent && $this->errorBubbling) {
  368. $this->parent->addError($error);
  369. } else {
  370. $this->errors[] = $error;
  371. }
  372. }
  373. /**
  374. * Returns whether errors bubble up to the parent
  375. *
  376. * @return Boolean
  377. */
  378. public function getErrorBubbling()
  379. {
  380. return $this->errorBubbling;
  381. }
  382. /**
  383. * Returns whether the field is bound.
  384. *
  385. * @return Boolean true if the form is bound to input values, false otherwise
  386. */
  387. public function isBound()
  388. {
  389. return $this->bound;
  390. }
  391. /**
  392. * Returns whether the data in the different formats is synchronized
  393. *
  394. * @return Boolean
  395. */
  396. public function isSynchronized()
  397. {
  398. return $this->synchronized;
  399. }
  400. /**
  401. * {@inheritDoc}
  402. */
  403. public function isEmpty()
  404. {
  405. foreach ($this->children as $child) {
  406. if (!$child->isEmpty()) {
  407. return false;
  408. }
  409. }
  410. return array() === $this->data || null === $this->data || '' === $this->data;
  411. }
  412. /**
  413. * Returns whether the field is valid.
  414. *
  415. * @return Boolean
  416. */
  417. public function isValid()
  418. {
  419. // TESTME
  420. if (!$this->isBound() || $this->hasErrors()) {
  421. return false;
  422. }
  423. foreach ($this->children as $child) {
  424. if (!$child->isValid()) {
  425. return false;
  426. }
  427. }
  428. return true;
  429. }
  430. /**
  431. * Returns whether or not there are errors.
  432. *
  433. * @return Boolean true if form is bound and not valid
  434. */
  435. public function hasErrors()
  436. {
  437. // Don't call isValid() here, as its semantics are slightly different
  438. // Field groups are not valid if their children are invalid, but
  439. // hasErrors() returns only true if a field/field group itself has
  440. // errors
  441. return count($this->errors) > 0;
  442. }
  443. /**
  444. * Returns all errors
  445. *
  446. * @return array An array of FormError instances that occurred during binding
  447. */
  448. public function getErrors()
  449. {
  450. return $this->errors;
  451. }
  452. /**
  453. * Returns the DataTransformer.
  454. *
  455. * @return DataTransformerInterface
  456. */
  457. public function getNormTransformer()
  458. {
  459. return $this->normTransformer;
  460. }
  461. /**
  462. * Returns the DataTransformer.
  463. *
  464. * @return DataTransformerInterface
  465. */
  466. public function getClientTransformer()
  467. {
  468. return $this->clientTransformer;
  469. }
  470. /**
  471. * Returns the renderer
  472. *
  473. * @return FormRendererInterface
  474. */
  475. public function getRenderer()
  476. {
  477. return $this->renderer;
  478. }
  479. /**
  480. * Returns all children in this group
  481. *
  482. * @return array
  483. */
  484. public function getChildren()
  485. {
  486. return $this->children;
  487. }
  488. public function hasChildren()
  489. {
  490. return count($this->children) > 0;
  491. }
  492. public function add(FormInterface $child)
  493. {
  494. $this->children[$child->getName()] = $child;
  495. $child->setParent($this);
  496. if ($this->dataMapper) {
  497. $this->dataMapper->mapDataToForm($this->getClientData(), $child);
  498. }
  499. }
  500. public function remove($name)
  501. {
  502. if (isset($this->children[$name])) {
  503. $this->children[$name]->setParent(null);
  504. unset($this->children[$name]);
  505. }
  506. }
  507. /**
  508. * Returns whether a child with the given name exists.
  509. *
  510. * @param string $name
  511. * @return Boolean
  512. */
  513. public function has($name)
  514. {
  515. return isset($this->children[$name]);
  516. }
  517. /**
  518. * Returns the child with the given name.
  519. *
  520. * @param string $name
  521. * @return FormInterface
  522. */
  523. public function get($name)
  524. {
  525. if (isset($this->children[$name])) {
  526. return $this->children[$name];
  527. }
  528. throw new \InvalidArgumentException(sprintf('Field "%s" does not exist.', $name));
  529. }
  530. /**
  531. * Returns true if the child exists (implements the \ArrayAccess interface).
  532. *
  533. * @param string $name The name of the child
  534. *
  535. * @return Boolean true if the widget exists, false otherwise
  536. */
  537. public function offsetExists($name)
  538. {
  539. return $this->has($name);
  540. }
  541. /**
  542. * Returns the form child associated with the name (implements the \ArrayAccess interface).
  543. *
  544. * @param string $name The offset of the value to get
  545. *
  546. * @return Field A form child instance
  547. */
  548. public function offsetGet($name)
  549. {
  550. return $this->get($name);
  551. }
  552. /**
  553. * Throws an exception saying that values cannot be set (implements the \ArrayAccess interface).
  554. *
  555. * @param string $offset (ignored)
  556. * @param string $value (ignored)
  557. *
  558. * @throws \LogicException
  559. */
  560. public function offsetSet($name, $child)
  561. {
  562. throw new \BadMethodCallException('offsetSet() is not supported');
  563. }
  564. /**
  565. * Throws an exception saying that values cannot be unset (implements the \ArrayAccess interface).
  566. *
  567. * @param string $name
  568. *
  569. * @throws \LogicException
  570. */
  571. public function offsetUnset($name)
  572. {
  573. throw new \BadMethodCallException('offsetUnset() is not supported');
  574. }
  575. /**
  576. * Returns the iterator for this group.
  577. *
  578. * @return \ArrayIterator
  579. */
  580. public function getIterator()
  581. {
  582. return new \ArrayIterator($this->children);
  583. }
  584. /**
  585. * Returns the number of form children (implements the \Countable interface).
  586. *
  587. * @return integer The number of embedded form children
  588. */
  589. public function count()
  590. {
  591. return count($this->children);
  592. }
  593. /**
  594. * Normalizes the value if a normalization transformer is set
  595. *
  596. * @param mixed $value The value to transform
  597. * @return string
  598. */
  599. private function appToNorm($value)
  600. {
  601. if (null === $this->normTransformer) {
  602. return $value;
  603. }
  604. return $this->normTransformer->transform($value);
  605. }
  606. /**
  607. * Reverse transforms a value if a normalization transformer is set.
  608. *
  609. * @param string $value The value to reverse transform
  610. * @return mixed
  611. */
  612. private function normToApp($value)
  613. {
  614. if (null === $this->normTransformer) {
  615. return $value;
  616. }
  617. return $this->normTransformer->reverseTransform($value);
  618. }
  619. /**
  620. * Transforms the value if a value transformer is set.
  621. *
  622. * @param mixed $value The value to transform
  623. * @return string
  624. */
  625. private function normToClient($value)
  626. {
  627. if (null === $this->clientTransformer) {
  628. // Scalar values should always be converted to strings to
  629. // facilitate differentiation between empty ("") and zero (0).
  630. return null === $value || is_scalar($value) ? (string)$value : $value;
  631. }
  632. return $this->clientTransformer->transform($value);
  633. }
  634. /**
  635. * Reverse transforms a value if a value transformer is set.
  636. *
  637. * @param string $value The value to reverse transform
  638. * @return mixed
  639. */
  640. private function clientToNorm($value)
  641. {
  642. if (null === $this->clientTransformer) {
  643. return '' === $value ? null : $value;
  644. }
  645. return $this->clientTransformer->reverseTransform($value);
  646. }
  647. /**
  648. * Binds a request to the form
  649. *
  650. * If the request was a POST request, the data is bound to the form,
  651. * transformed and written into the form data (an object or an array).
  652. * You can set the form data by passing it in the second parameter
  653. * of this method or by passing it in the "data" option of the form's
  654. * constructor.
  655. *
  656. * @param Request $request The request to bind to the form
  657. * @param array|object $data The data from which to read default values
  658. * and where to write bound values
  659. */
  660. public function bindRequest(Request $request)
  661. {
  662. // Store the bound data in case of a post request
  663. switch ($request->getMethod()) {
  664. case 'POST':
  665. case 'PUT':
  666. $data = array_replace_recursive(
  667. $request->request->get($this->getName(), array()),
  668. $request->files->get($this->getName(), array())
  669. );
  670. break;
  671. case 'GET':
  672. $data = $request->query->get($this->getName(), array());
  673. break;
  674. default:
  675. throw new FormException(sprintf('The request method "%s" is not supported', $request->getMethod()));
  676. }
  677. $this->bind($data);
  678. }
  679. /**
  680. * Validates the data of this form
  681. *
  682. * This method is called automatically during the validation process.
  683. *
  684. * @param ExecutionContext $context The current validation context
  685. * @deprecated
  686. */
  687. public function validateData(ExecutionContext $context)
  688. {
  689. if (is_object($this->getData()) || is_array($this->getData())) {
  690. $groups = $this->getAttribute('validation_groups');
  691. $child = $this;
  692. while (!$groups && $child->hasParent()) {
  693. $child = $child->getParent();
  694. $groups = $child->getAttribute('validation_groups');
  695. }
  696. if (null === $groups) {
  697. $groups = array(null);
  698. }
  699. $propertyPath = $context->getPropertyPath();
  700. $graphWalker = $context->getGraphWalker();
  701. // The Execute constraint is called on class level, so we need to
  702. // set the property manually
  703. $context->setCurrentProperty('data');
  704. // Adjust the property path accordingly
  705. if (!empty($propertyPath)) {
  706. $propertyPath .= '.';
  707. }
  708. $propertyPath .= 'data';
  709. foreach ($groups as $group) {
  710. $graphWalker->walkReference($this->getData(), $group, $propertyPath, true);
  711. }
  712. }
  713. }
  714. }