FormInterface.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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\Form\Exception\TransformationFailedException;
  12. /**
  13. * A form group bundling multiple forms in a hierarchical structure.
  14. *
  15. * @author Bernhard Schussek <bschussek@gmail.com>
  16. */
  17. interface FormInterface extends \ArrayAccess, \Traversable, \Countable
  18. {
  19. /**
  20. * Sets the parent form.
  21. *
  22. * @return self
  23. *
  24. * @throws Exception\AlreadySubmittedException if the form has already been submitted
  25. * @throws Exception\LogicException when trying to set a parent for a form with
  26. * an empty name
  27. */
  28. public function setParent(FormInterface $parent = null);
  29. /**
  30. * Returns the parent form.
  31. *
  32. * @return self|null The parent form or null if there is none
  33. */
  34. public function getParent();
  35. /**
  36. * Adds or replaces a child to the form.
  37. *
  38. * @param FormInterface|string|int $child The FormInterface instance or the name of the child
  39. * @param string|null $type The child's type, if a name was passed
  40. * @param array $options The child's options, if a name was passed
  41. *
  42. * @return self
  43. *
  44. * @throws Exception\AlreadySubmittedException if the form has already been submitted
  45. * @throws Exception\LogicException when trying to add a child to a non-compound form
  46. * @throws Exception\UnexpectedTypeException if $child or $type has an unexpected type
  47. */
  48. public function add($child, $type = null, array $options = array());
  49. /**
  50. * Returns the child with the given name.
  51. *
  52. * @param string $name The name of the child
  53. *
  54. * @return self
  55. *
  56. * @throws \OutOfBoundsException if the named child does not exist
  57. */
  58. public function get($name);
  59. /**
  60. * Returns whether a child with the given name exists.
  61. *
  62. * @param string $name The name of the child
  63. *
  64. * @return bool
  65. */
  66. public function has($name);
  67. /**
  68. * Removes a child from the form.
  69. *
  70. * @param string $name The name of the child to remove
  71. *
  72. * @return $this
  73. *
  74. * @throws Exception\AlreadySubmittedException if the form has already been submitted
  75. */
  76. public function remove($name);
  77. /**
  78. * Returns all children in this group.
  79. *
  80. * @return self[]
  81. */
  82. public function all();
  83. /**
  84. * Returns the errors of this form.
  85. *
  86. * @param bool $deep Whether to include errors of child forms as well
  87. * @param bool $flatten Whether to flatten the list of errors in case
  88. * $deep is set to true
  89. *
  90. * @return FormErrorIterator An iterator over the {@link FormError}
  91. * instances that where added to this form
  92. */
  93. public function getErrors($deep = false, $flatten = true);
  94. /**
  95. * Updates the form with default data.
  96. *
  97. * @param mixed $modelData The data formatted as expected for the underlying object
  98. *
  99. * @return $this
  100. *
  101. * @throws Exception\AlreadySubmittedException if the form has already been submitted
  102. * @throws Exception\LogicException If listeners try to call setData in a cycle. Or if
  103. * the view data does not match the expected type
  104. * according to {@link FormConfigInterface::getDataClass}.
  105. */
  106. public function setData($modelData);
  107. /**
  108. * Returns the data in the format needed for the underlying object.
  109. *
  110. * @return mixed
  111. */
  112. public function getData();
  113. /**
  114. * Returns the normalized data of the field.
  115. *
  116. * @return mixed When the field is not submitted, the default data is returned.
  117. * When the field is submitted, the normalized submitted data is
  118. * returned if the field is valid, null otherwise.
  119. */
  120. public function getNormData();
  121. /**
  122. * Returns the data transformed by the value transformer.
  123. *
  124. * @return mixed
  125. */
  126. public function getViewData();
  127. /**
  128. * Returns the extra data.
  129. *
  130. * @return array The submitted data which do not belong to a child
  131. */
  132. public function getExtraData();
  133. /**
  134. * Returns the form's configuration.
  135. *
  136. * @return FormConfigInterface The configuration
  137. */
  138. public function getConfig();
  139. /**
  140. * Returns whether the form is submitted.
  141. *
  142. * @return bool true if the form is submitted, false otherwise
  143. */
  144. public function isSubmitted();
  145. /**
  146. * Returns the name by which the form is identified in forms.
  147. *
  148. * @return string The name of the form
  149. */
  150. public function getName();
  151. /**
  152. * Returns the property path that the form is mapped to.
  153. *
  154. * @return \Symfony\Component\PropertyAccess\PropertyPathInterface|null The property path
  155. */
  156. public function getPropertyPath();
  157. /**
  158. * Adds an error to this form.
  159. *
  160. * @param FormError $error
  161. *
  162. * @return $this
  163. */
  164. public function addError(FormError $error);
  165. /**
  166. * Returns whether the form and all children are valid.
  167. *
  168. * If the form is not submitted, this method always returns false.
  169. *
  170. * @return bool
  171. */
  172. public function isValid();
  173. /**
  174. * Returns whether the form is required to be filled out.
  175. *
  176. * If the form has a parent and the parent is not required, this method
  177. * will always return false. Otherwise the value set with setRequired()
  178. * is returned.
  179. *
  180. * @return bool
  181. */
  182. public function isRequired();
  183. /**
  184. * Returns whether this form is disabled.
  185. *
  186. * The content of a disabled form is displayed, but not allowed to be
  187. * modified. The validation of modified disabled forms should fail.
  188. *
  189. * Forms whose parents are disabled are considered disabled regardless of
  190. * their own state.
  191. *
  192. * @return bool
  193. */
  194. public function isDisabled();
  195. /**
  196. * Returns whether the form is empty.
  197. *
  198. * @return bool
  199. */
  200. public function isEmpty();
  201. /**
  202. * Returns whether the data in the different formats is synchronized.
  203. *
  204. * If the data is not synchronized, you can get the transformation failure
  205. * by calling {@link getTransformationFailure()}.
  206. *
  207. * @return bool
  208. */
  209. public function isSynchronized();
  210. /**
  211. * Returns the data transformation failure, if any.
  212. *
  213. * @return TransformationFailedException|null The transformation failure
  214. */
  215. public function getTransformationFailure();
  216. /**
  217. * Initializes the form tree.
  218. *
  219. * Should be called on the root form after constructing the tree.
  220. *
  221. * @return $this
  222. */
  223. public function initialize();
  224. /**
  225. * Inspects the given request and calls {@link submit()} if the form was
  226. * submitted.
  227. *
  228. * Internally, the request is forwarded to the configured
  229. * {@link RequestHandlerInterface} instance, which determines whether to
  230. * submit the form or not.
  231. *
  232. * @param mixed $request The request to handle
  233. *
  234. * @return $this
  235. */
  236. public function handleRequest($request = null);
  237. /**
  238. * Submits data to the form, transforms and validates it.
  239. *
  240. * @param mixed $submittedData The submitted data
  241. * @param bool $clearMissing Whether to set fields to NULL when they
  242. * are missing in the submitted data
  243. *
  244. * @return $this
  245. *
  246. * @throws Exception\AlreadySubmittedException if the form has already been submitted
  247. */
  248. public function submit($submittedData, $clearMissing = true);
  249. /**
  250. * Returns the root of the form tree.
  251. *
  252. * @return self The root of the tree
  253. */
  254. public function getRoot();
  255. /**
  256. * Returns whether the field is the root of the form tree.
  257. *
  258. * @return bool
  259. */
  260. public function isRoot();
  261. /**
  262. * Creates a view.
  263. *
  264. * @return FormView The view
  265. */
  266. public function createView(FormView $parent = null);
  267. }