FieldInterface.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. <?php
  2. namespace Symfony\Component\Form;
  3. /*
  4. * This file is part of the Symfony framework.
  5. *
  6. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  7. *
  8. * This source file is subject to the MIT license that is bundled
  9. * with this source code in the file LICENSE.
  10. */
  11. use Symfony\Component\I18N\TranslatorInterface;
  12. /**
  13. * A form field that can be embedded in a form.
  14. *
  15. * @author Bernhard Schussek <bernhard.schussek@symfony-project.com>
  16. */
  17. interface FieldInterface
  18. {
  19. /**
  20. * Marks a constraint violation in a form field
  21. * @var integer
  22. */
  23. const FIELD_ERROR = 0;
  24. /**
  25. * Marks a constraint violation in the data of a form field
  26. * @var integer
  27. */
  28. const DATA_ERROR = 1;
  29. /**
  30. * Clones this field.
  31. */
  32. function __clone();
  33. /**
  34. * Sets the parent field.
  35. *
  36. * @param FieldInterface $parent The parent field
  37. */
  38. function setParent(FieldInterface $parent = null);
  39. /**
  40. * Returns the parent field.
  41. *
  42. * @return FieldInterface The parent field
  43. */
  44. function getParent();
  45. /**
  46. * Sets the key by which the field is identified in field groups.
  47. *
  48. * Once this field is nested in a field group, i.e. after setParent() was
  49. * called for the first time, this method should throw an exception.
  50. *
  51. * @param string $key The key of the field
  52. * @throws BadMethodCallException When the field already has a parent
  53. */
  54. function setKey($key);
  55. /**
  56. * Returns the key by which the field is identified in field groups.
  57. *
  58. * @return string The key of the field.
  59. */
  60. function getKey();
  61. /**
  62. * Returns the name of the field.
  63. *
  64. * @return string When the field has no parent, the name is equal to its
  65. * key. If the field has a parent, the name is composed of
  66. * the parent's name and the field's key, where the field's
  67. * key is wrapped in squared brackets
  68. * (e.g. "parent_name[field_key]")
  69. */
  70. function getName();
  71. /**
  72. * Returns the ID of the field.
  73. *
  74. * @return string The ID of a field is equal to its name, where all
  75. * sequences of squared brackets are replaced by a single
  76. * underscore (e.g. if the name is "parent_name[field_key]",
  77. * the ID is "parent_name_field_key").
  78. */
  79. function getId();
  80. /**
  81. * Sets the property path
  82. *
  83. * The property path determines the property or a sequence of properties
  84. * that a field updates in the data of the field group.
  85. *
  86. * @param string $propertyPath
  87. */
  88. function setPropertyPath($propertyPath);
  89. /**
  90. * Returns the property path of the field
  91. *
  92. * @return PropertyPath
  93. */
  94. function getPropertyPath();
  95. /**
  96. * Writes a property value of the object into the field
  97. *
  98. * The chosen property is determined by the field's property path.
  99. *
  100. * @param array|object $objectOrArray
  101. */
  102. function updateFromProperty(&$objectOrArray);
  103. /**
  104. * Writes a the field value into a property of the object
  105. *
  106. * The chosen property is determined by the field's property path.
  107. *
  108. * @param array|object $objectOrArray
  109. */
  110. function updateProperty(&$objectOrArray);
  111. /**
  112. * Returns the normalized data of the field.
  113. *
  114. * @return mixed When the field is not bound, the default data is returned.
  115. * When the field is bound, the normalized bound data is
  116. * returned if the field is valid, null otherwise.
  117. */
  118. function getData();
  119. /**
  120. * Returns the data of the field as it is displayed to the user.
  121. *
  122. * @return string|array When the field is not bound, the transformed
  123. * default data is returned. When the field is bound,
  124. * the bound data is returned.
  125. */
  126. function getDisplayedData();
  127. /**
  128. * Sets the default data
  129. *
  130. * @param mixed $default The default data
  131. * @throws UnexpectedTypeException If the default data is invalid
  132. */
  133. function setData($default);
  134. /**
  135. * Binds POST data to the field, transforms and validates it.
  136. *
  137. * @param string|array $taintedData The POST data
  138. * @return boolean Whether the form is valid
  139. * @throws InvalidConfigurationException when the field is not configured
  140. * correctly
  141. */
  142. function bind($taintedData);
  143. /**
  144. * Recursively adds constraint violations to the fields
  145. *
  146. * Violations in the form fields usually have property paths like:
  147. *
  148. * <code>
  149. * iterator[firstName].data
  150. * iterator[firstName].displayedData
  151. * iterator[Address].iterator[street].displayedData
  152. * ...
  153. * </code>
  154. *
  155. * Violations in the form data usually have property paths like:
  156. *
  157. * <code>
  158. * data.firstName
  159. * data.Address.street
  160. * ...
  161. * </code>
  162. *
  163. * @param FieldError $error
  164. * @param PropertyPathIterator $pathIterator
  165. * @param ConstraintViolation$violation
  166. */
  167. function addError(FieldError $error, PropertyPathIterator $pathIterator = null, $type = null);
  168. /**
  169. * Returns whether the field is bound.
  170. *
  171. * @return boolean
  172. */
  173. function isBound();
  174. /**
  175. * Returns whether the field is valid.
  176. *
  177. * @return boolean
  178. */
  179. function isValid();
  180. /**
  181. * Returns whether the field requires a multipart form.
  182. *
  183. * @return boolean
  184. */
  185. function isMultipart();
  186. /**
  187. * Returns whether the field is required to be filled out.
  188. *
  189. * If the field has a parent and the parent is not required, this method
  190. * will always return false. Otherwise the value set with setRequired()
  191. * is returned.
  192. *
  193. * @return boolean
  194. */
  195. function isRequired();
  196. /**
  197. * Returns whether this field is disabled
  198. *
  199. * The content of a disabled field is displayed, but not allowed to be
  200. * modified. The validation of modified, disabled fields should fail.
  201. *
  202. * Fields whose parents are disabled are considered disabled regardless of
  203. * their own state.
  204. *
  205. * @return boolean
  206. */
  207. function isDisabled();
  208. /**
  209. * Returns whether the field is hidden
  210. *
  211. * @return boolean
  212. */
  213. function isHidden();
  214. /**
  215. * Sets whether this field is required to be filled out when submitted.
  216. *
  217. * @param boolean $required
  218. */
  219. function setRequired($required);
  220. }