Button.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  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\AlreadySubmittedException;
  12. use Symfony\Component\Form\Exception\BadMethodCallException;
  13. /**
  14. * A form button.
  15. *
  16. * @author Bernhard Schussek <bschussek@gmail.com>
  17. */
  18. class Button implements \IteratorAggregate, FormInterface
  19. {
  20. /**
  21. * @var FormInterface|null
  22. */
  23. private $parent;
  24. /**
  25. * @var FormConfigInterface
  26. */
  27. private $config;
  28. /**
  29. * @var bool
  30. */
  31. private $submitted = false;
  32. /**
  33. * Creates a new button from a form configuration.
  34. *
  35. * @param FormConfigInterface $config The button's configuration
  36. */
  37. public function __construct(FormConfigInterface $config)
  38. {
  39. $this->config = $config;
  40. }
  41. /**
  42. * Unsupported method.
  43. *
  44. * @param mixed $offset
  45. *
  46. * @return bool Always returns false
  47. */
  48. public function offsetExists($offset)
  49. {
  50. return false;
  51. }
  52. /**
  53. * Unsupported method.
  54. *
  55. * This method should not be invoked.
  56. *
  57. * @param mixed $offset
  58. *
  59. * @throws BadMethodCallException
  60. */
  61. public function offsetGet($offset)
  62. {
  63. throw new BadMethodCallException('Buttons cannot have children.');
  64. }
  65. /**
  66. * Unsupported method.
  67. *
  68. * This method should not be invoked.
  69. *
  70. * @param mixed $offset
  71. * @param mixed $value
  72. *
  73. * @throws BadMethodCallException
  74. */
  75. public function offsetSet($offset, $value)
  76. {
  77. throw new BadMethodCallException('Buttons cannot have children.');
  78. }
  79. /**
  80. * Unsupported method.
  81. *
  82. * This method should not be invoked.
  83. *
  84. * @param mixed $offset
  85. *
  86. * @throws BadMethodCallException
  87. */
  88. public function offsetUnset($offset)
  89. {
  90. throw new BadMethodCallException('Buttons cannot have children.');
  91. }
  92. /**
  93. * {@inheritdoc}
  94. */
  95. public function setParent(FormInterface $parent = null)
  96. {
  97. if ($this->submitted) {
  98. throw new AlreadySubmittedException('You cannot set the parent of a submitted button');
  99. }
  100. $this->parent = $parent;
  101. }
  102. /**
  103. * {@inheritdoc}
  104. */
  105. public function getParent()
  106. {
  107. return $this->parent;
  108. }
  109. /**
  110. * Unsupported method.
  111. *
  112. * This method should not be invoked.
  113. *
  114. * @param int|string|FormInterface $child
  115. * @param null $type
  116. * @param array $options
  117. *
  118. * @throws BadMethodCallException
  119. */
  120. public function add($child, $type = null, array $options = array())
  121. {
  122. throw new BadMethodCallException('Buttons cannot have children.');
  123. }
  124. /**
  125. * Unsupported method.
  126. *
  127. * This method should not be invoked.
  128. *
  129. * @param string $name
  130. *
  131. * @throws BadMethodCallException
  132. */
  133. public function get($name)
  134. {
  135. throw new BadMethodCallException('Buttons cannot have children.');
  136. }
  137. /**
  138. * Unsupported method.
  139. *
  140. * @param string $name
  141. *
  142. * @return bool Always returns false
  143. */
  144. public function has($name)
  145. {
  146. return false;
  147. }
  148. /**
  149. * Unsupported method.
  150. *
  151. * This method should not be invoked.
  152. *
  153. * @param string $name
  154. *
  155. * @throws BadMethodCallException
  156. */
  157. public function remove($name)
  158. {
  159. throw new BadMethodCallException('Buttons cannot have children.');
  160. }
  161. /**
  162. * {@inheritdoc}
  163. */
  164. public function all()
  165. {
  166. return array();
  167. }
  168. /**
  169. * {@inheritdoc}
  170. */
  171. public function getErrors($deep = false, $flatten = true)
  172. {
  173. return new FormErrorIterator($this, array());
  174. }
  175. /**
  176. * Unsupported method.
  177. *
  178. * This method should not be invoked.
  179. *
  180. * @param mixed $modelData
  181. */
  182. public function setData($modelData)
  183. {
  184. // called during initialization of the form tree
  185. // noop
  186. }
  187. /**
  188. * Unsupported method.
  189. */
  190. public function getData()
  191. {
  192. }
  193. /**
  194. * Unsupported method.
  195. */
  196. public function getNormData()
  197. {
  198. }
  199. /**
  200. * Unsupported method.
  201. */
  202. public function getViewData()
  203. {
  204. }
  205. /**
  206. * Unsupported method.
  207. *
  208. * @return array Always returns an empty array
  209. */
  210. public function getExtraData()
  211. {
  212. return array();
  213. }
  214. /**
  215. * Returns the button's configuration.
  216. *
  217. * @return FormConfigInterface The configuration
  218. */
  219. public function getConfig()
  220. {
  221. return $this->config;
  222. }
  223. /**
  224. * Returns whether the button is submitted.
  225. *
  226. * @return bool true if the button was submitted
  227. */
  228. public function isSubmitted()
  229. {
  230. return $this->submitted;
  231. }
  232. /**
  233. * Returns the name by which the button is identified in forms.
  234. *
  235. * @return string The name of the button
  236. */
  237. public function getName()
  238. {
  239. return $this->config->getName();
  240. }
  241. /**
  242. * Unsupported method.
  243. */
  244. public function getPropertyPath()
  245. {
  246. }
  247. /**
  248. * Unsupported method.
  249. *
  250. * @throws BadMethodCallException
  251. */
  252. public function addError(FormError $error)
  253. {
  254. throw new BadMethodCallException('Buttons cannot have errors.');
  255. }
  256. /**
  257. * Unsupported method.
  258. *
  259. * @return bool Always returns true
  260. */
  261. public function isValid()
  262. {
  263. return true;
  264. }
  265. /**
  266. * Unsupported method.
  267. *
  268. * @return bool Always returns false
  269. */
  270. public function isRequired()
  271. {
  272. return false;
  273. }
  274. /**
  275. * {@inheritdoc}
  276. */
  277. public function isDisabled()
  278. {
  279. if (null === $this->parent || !$this->parent->isDisabled()) {
  280. return $this->config->getDisabled();
  281. }
  282. return true;
  283. }
  284. /**
  285. * Unsupported method.
  286. *
  287. * @return bool Always returns true
  288. */
  289. public function isEmpty()
  290. {
  291. return true;
  292. }
  293. /**
  294. * Unsupported method.
  295. *
  296. * @return bool Always returns true
  297. */
  298. public function isSynchronized()
  299. {
  300. return true;
  301. }
  302. /**
  303. * Unsupported method.
  304. */
  305. public function getTransformationFailure()
  306. {
  307. }
  308. /**
  309. * Unsupported method.
  310. *
  311. * @throws BadMethodCallException
  312. */
  313. public function initialize()
  314. {
  315. throw new BadMethodCallException('Buttons cannot be initialized. Call initialize() on the root form instead.');
  316. }
  317. /**
  318. * Unsupported method.
  319. *
  320. * @param mixed $request
  321. *
  322. * @throws BadMethodCallException
  323. */
  324. public function handleRequest($request = null)
  325. {
  326. throw new BadMethodCallException('Buttons cannot handle requests. Call handleRequest() on the root form instead.');
  327. }
  328. /**
  329. * Submits data to the button.
  330. *
  331. * @param string|null $submittedData The data
  332. * @param bool $clearMissing Not used
  333. *
  334. * @return $this
  335. *
  336. * @throws Exception\AlreadySubmittedException if the button has already been submitted
  337. */
  338. public function submit($submittedData, $clearMissing = true)
  339. {
  340. if ($this->submitted) {
  341. throw new AlreadySubmittedException('A form can only be submitted once');
  342. }
  343. $this->submitted = true;
  344. return $this;
  345. }
  346. /**
  347. * {@inheritdoc}
  348. */
  349. public function getRoot()
  350. {
  351. return $this->parent ? $this->parent->getRoot() : $this;
  352. }
  353. /**
  354. * {@inheritdoc}
  355. */
  356. public function isRoot()
  357. {
  358. return null === $this->parent;
  359. }
  360. /**
  361. * {@inheritdoc}
  362. */
  363. public function createView(FormView $parent = null)
  364. {
  365. if (null === $parent && $this->parent) {
  366. $parent = $this->parent->createView();
  367. }
  368. $type = $this->config->getType();
  369. $options = $this->config->getOptions();
  370. $view = $type->createView($this, $parent);
  371. $type->buildView($view, $this, $options);
  372. $type->finishView($view, $this, $options);
  373. return $view;
  374. }
  375. /**
  376. * Unsupported method.
  377. *
  378. * @return int Always returns 0
  379. */
  380. public function count()
  381. {
  382. return 0;
  383. }
  384. /**
  385. * Unsupported method.
  386. *
  387. * @return \EmptyIterator Always returns an empty iterator
  388. */
  389. public function getIterator()
  390. {
  391. return new \EmptyIterator();
  392. }
  393. }