FormBuilder.php 11 KB

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