FormBuilder.php 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  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\DataMapper\DataMapperInterface;
  12. use Symfony\Component\Form\DataTransformer\DataTransformerInterface;
  13. use Symfony\Component\Form\Validator\FormValidatorInterface;
  14. use Symfony\Component\Form\Exception\FormException;
  15. use Symfony\Component\Form\Exception\UnexpectedTypeException;
  16. use Symfony\Component\Form\Type\FormTypeInterface;
  17. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  18. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  19. class FormBuilder
  20. {
  21. private $name;
  22. private $data;
  23. private $dispatcher;
  24. private $factory;
  25. private $readOnly;
  26. private $required;
  27. private $clientTransformer;
  28. private $normalizationTransformer;
  29. private $validators = array();
  30. private $attributes = array();
  31. private $types = array();
  32. private $parent;
  33. private $dataClass;
  34. private $children = array();
  35. private $dataMapper;
  36. private $errorBubbling = false;
  37. private $emptyData = null;
  38. public function __construct($name, EventDispatcherInterface $dispatcher, $dataClass = null)
  39. {
  40. $this->name = $name;
  41. $this->dispatcher = $dispatcher;
  42. $this->dataClass = $dataClass;
  43. }
  44. public function setFormFactory(FormFactoryInterface $factory)
  45. {
  46. $this->factory = $factory;
  47. return $this;
  48. }
  49. public function getFormFactory()
  50. {
  51. return $this->factory;
  52. }
  53. public function getName()
  54. {
  55. return $this->name;
  56. }
  57. public function setParent(FormBuilder $builder)
  58. {
  59. $this->parent = $builder;
  60. return $this;
  61. }
  62. public function getParent()
  63. {
  64. return $this->parent;
  65. }
  66. public function end()
  67. {
  68. return $this->parent;
  69. }
  70. public function setData($data)
  71. {
  72. $this->data = $data;
  73. return $this;
  74. }
  75. public function getData()
  76. {
  77. return $this->data;
  78. }
  79. public function setReadOnly($readOnly)
  80. {
  81. $this->readOnly = $readOnly;
  82. return $this;
  83. }
  84. public function getReadOnly()
  85. {
  86. return $this->readOnly;
  87. }
  88. /**
  89. * Sets whether this field is required to be filled out when bound.
  90. *
  91. * @param Boolean $required
  92. */
  93. public function setRequired($required)
  94. {
  95. $this->required = $required;
  96. return $this;
  97. }
  98. public function getRequired()
  99. {
  100. return $this->required;
  101. }
  102. public function setErrorBubbling($errorBubbling)
  103. {
  104. $this->errorBubbling = $errorBubbling;
  105. return $this;
  106. }
  107. public function getErrorBubbling()
  108. {
  109. return $this->errorBubbling;
  110. }
  111. public function addValidator(FormValidatorInterface $validator)
  112. {
  113. $this->validators[] = $validator;
  114. return $this;
  115. }
  116. public function getValidators()
  117. {
  118. return $this->validators;
  119. }
  120. /**
  121. * Adds an event listener for events on this field
  122. *
  123. * @see Symfony\Component\EventDispatcher\EventDispatcherInterface::addEventListener
  124. */
  125. public function addEventListener($eventNames, $listener, $priority = 0)
  126. {
  127. $this->dispatcher->addListener($eventNames, $listener, $priority);
  128. return $this;
  129. }
  130. /**
  131. * Adds an event subscriber for events on this field
  132. *
  133. * @see Symfony\Component\EventDispatcher\EventDispatcherInterface::addEventSubscriber
  134. */
  135. public function addEventSubscriber(EventSubscriberInterface $subscriber, $priority = 0)
  136. {
  137. $this->dispatcher->addSubscriber($subscriber, $priority);
  138. return $this;
  139. }
  140. protected function buildDispatcher()
  141. {
  142. return $this->dispatcher;
  143. }
  144. /**
  145. * Sets the DataTransformer.
  146. *
  147. * @param DataTransformerInterface $clientTransformer
  148. */
  149. public function setNormTransformer(DataTransformerInterface $normalizationTransformer = null)
  150. {
  151. $this->normalizationTransformer = $normalizationTransformer;
  152. return $this;
  153. }
  154. public function getNormTransformer()
  155. {
  156. return $this->normalizationTransformer;
  157. }
  158. /**
  159. * Sets the DataTransformer.
  160. *
  161. * @param DataTransformerInterface $clientTransformer
  162. */
  163. public function setClientTransformer(DataTransformerInterface $clientTransformer = null)
  164. {
  165. $this->clientTransformer = $clientTransformer;
  166. return $this;
  167. }
  168. public function getClientTransformer()
  169. {
  170. return $this->clientTransformer;
  171. }
  172. public function setAttribute($name, $value)
  173. {
  174. $this->attributes[$name] = $value;
  175. return $this;
  176. }
  177. public function getAttribute($name)
  178. {
  179. return $this->attributes[$name];
  180. }
  181. public function hasAttribute($name)
  182. {
  183. return isset($this->attributes[$name]);
  184. }
  185. public function getAttributes()
  186. {
  187. return $this->attributes;
  188. }
  189. public function setDataMapper(DataMapperInterface $dataMapper)
  190. {
  191. $this->dataMapper = $dataMapper;
  192. }
  193. public function getDataMapper()
  194. {
  195. return $this->dataMapper;
  196. }
  197. public function setTypes(array $types)
  198. {
  199. $this->types = $types;
  200. return $this;
  201. }
  202. public function getTypes()
  203. {
  204. return $this->types;
  205. }
  206. public function setEmptyData($emptyData)
  207. {
  208. $this->emptyData = $emptyData;
  209. return $this;
  210. }
  211. public function getEmptyData()
  212. {
  213. return $this->emptyData;
  214. }
  215. /**
  216. * Adds a new field to this group. A field must have a unique name within
  217. * the group. Otherwise the existing field is overwritten.
  218. *
  219. * If you add a nested group, this group should also be represented in the
  220. * object hierarchy. If you want to add a group that operates on the same
  221. * hierarchy level, use merge().
  222. *
  223. * <code>
  224. * class Entity
  225. * {
  226. * public $location;
  227. * }
  228. *
  229. * class Location
  230. * {
  231. * public $longitude;
  232. * public $latitude;
  233. * }
  234. *
  235. * $entity = new Entity();
  236. * $entity->location = new Location();
  237. *
  238. * $form = new Form('entity', $entity, $validator);
  239. *
  240. * $locationGroup = new Form('location');
  241. * $locationGroup->add(new TextField('longitude'));
  242. * $locationGroup->add(new TextField('latitude'));
  243. *
  244. * $form->add($locationGroup);
  245. * </code>
  246. *
  247. * @param FormInterface|string $form
  248. * @return FormInterface
  249. */
  250. public function add($name, $type = null, array $options = array())
  251. {
  252. if (!is_string($name)) {
  253. throw new UnexpectedTypeException($name, 'string');
  254. }
  255. if (null !== $type && !is_string($type) && !$type instanceof FormTypeInterface) {
  256. throw new UnexpectedTypeException($type, 'string or Symfony\Component\Form\Type\FormTypeInterface');
  257. }
  258. $this->children[$name] = array(
  259. 'type' => $type,
  260. 'options' => $options,
  261. );
  262. return $this;
  263. }
  264. public function build($name, $type = null, array $options = array())
  265. {
  266. if (null !== $type) {
  267. $builder = $this->getFormFactory()->createBuilder(
  268. $type,
  269. $name,
  270. $options
  271. );
  272. } else {
  273. if (!$this->dataClass) {
  274. throw new FormException('The data class must be set to automatically create children');
  275. }
  276. $builder = $this->getFormFactory()->createBuilderForProperty(
  277. $this->dataClass,
  278. $name,
  279. $options
  280. );
  281. }
  282. $this->children[$name] = $builder;
  283. $builder->setParent($this);
  284. return $builder;
  285. }
  286. public function get($name)
  287. {
  288. if (!isset($this->children[$name])) {
  289. throw new FormException(sprintf('The field "%s" does not exist', $name));
  290. }
  291. $child = $this->children[$name];
  292. if ($child instanceof FormBuilder) {
  293. return $child;
  294. }
  295. return $this->build($name, $child['type'], $child['options']);
  296. }
  297. /**
  298. * Removes the field with the given name.
  299. *
  300. * @param string $name
  301. */
  302. public function remove($name)
  303. {
  304. if (isset($this->children[$name])) {
  305. // field might still be lazy
  306. if ($this->children[$name] instanceof FormInterface) {
  307. $this->children[$name]->setParent(null);
  308. }
  309. unset($this->children[$name]);
  310. }
  311. }
  312. /**
  313. * Returns whether a field with the given name exists.
  314. *
  315. * @param string $name
  316. * @return Boolean
  317. */
  318. public function has($name)
  319. {
  320. return isset($this->children[$name]);
  321. }
  322. protected function buildChildren()
  323. {
  324. $children = array();
  325. foreach ($this->children as $name => $builder) {
  326. if (!$builder instanceof FormBuilder) {
  327. $builder = $this->build($name, $builder['type'], $builder['options']);
  328. }
  329. $children[$builder->getName()] = $builder->getForm();
  330. }
  331. return $children;
  332. }
  333. public function getForm()
  334. {
  335. $instance = new Form(
  336. $this->getName(),
  337. $this->getTypes(),
  338. $this->buildDispatcher(),
  339. $this->getClientTransformer(),
  340. $this->getNormTransformer(),
  341. $this->getDataMapper(),
  342. $this->getValidators(),
  343. $this->getRequired(),
  344. $this->getReadOnly(),
  345. $this->getErrorBubbling(),
  346. $this->getEmptyData(),
  347. $this->getAttributes()
  348. );
  349. foreach ($this->buildChildren() as $child) {
  350. $instance->add($child);
  351. }
  352. if ($this->getData()) {
  353. $instance->setData($this->getData());
  354. }
  355. return $instance;
  356. }
  357. }