FormInterface.php 9.8 KB

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