Button.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  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
  22. */
  23. private $parent;
  24. /**
  25. * @var FormConfigInterface
  26. */
  27. private $config;
  28. /**
  29. * @var Boolean
  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 Boolean 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. $this->parent = $parent;
  98. }
  99. /**
  100. * {@inheritdoc}
  101. */
  102. public function getParent()
  103. {
  104. return $this->parent;
  105. }
  106. /**
  107. * Unsupported method.
  108. *
  109. * This method should not be invoked.
  110. *
  111. * @param int|string|FormInterface $child
  112. * @param null $type
  113. * @param array $options
  114. *
  115. * @throws BadMethodCallException
  116. */
  117. public function add($child, $type = null, array $options = array())
  118. {
  119. throw new BadMethodCallException('Buttons cannot have children.');
  120. }
  121. /**
  122. * Unsupported method.
  123. *
  124. * This method should not be invoked.
  125. *
  126. * @param string $name
  127. *
  128. * @throws BadMethodCallException
  129. */
  130. public function get($name)
  131. {
  132. throw new BadMethodCallException('Buttons cannot have children.');
  133. }
  134. /**
  135. * Unsupported method.
  136. *
  137. * @param string $name
  138. *
  139. * @return Boolean Always returns false.
  140. */
  141. public function has($name)
  142. {
  143. return false;
  144. }
  145. /**
  146. * Unsupported method.
  147. *
  148. * This method should not be invoked.
  149. *
  150. * @param string $name
  151. *
  152. * @throws BadMethodCallException
  153. */
  154. public function remove($name)
  155. {
  156. throw new BadMethodCallException('Buttons cannot have children.');
  157. }
  158. /**
  159. * {@inheritdoc}
  160. */
  161. public function all()
  162. {
  163. return array();
  164. }
  165. /**
  166. * {@inheritdoc}
  167. */
  168. public function getErrors()
  169. {
  170. return array();
  171. }
  172. /**
  173. * Unsupported method.
  174. *
  175. * This method should not be invoked.
  176. *
  177. * @param string $modelData
  178. *
  179. * @throws BadMethodCallException
  180. */
  181. public function setData($modelData)
  182. {
  183. // called during initialization of the form tree
  184. // noop
  185. }
  186. /**
  187. * Unsupported method.
  188. *
  189. * @return null Always returns null.
  190. */
  191. public function getData()
  192. {
  193. return null;
  194. }
  195. /**
  196. * Unsupported method.
  197. *
  198. * @return null Always returns null.
  199. */
  200. public function getNormData()
  201. {
  202. return null;
  203. }
  204. /**
  205. * Unsupported method.
  206. *
  207. * @return null Always returns null.
  208. */
  209. public function getViewData()
  210. {
  211. return null;
  212. }
  213. /**
  214. * Unsupported method.
  215. *
  216. * @return array Always returns an empty array.
  217. */
  218. public function getExtraData()
  219. {
  220. return array();
  221. }
  222. /**
  223. * Returns the button's configuration.
  224. *
  225. * @return FormConfigInterface The configuration.
  226. */
  227. public function getConfig()
  228. {
  229. return $this->config;
  230. }
  231. /**
  232. * Returns whether the button is submitted.
  233. *
  234. * @return Boolean true if the button was submitted.
  235. */
  236. public function isSubmitted()
  237. {
  238. return $this->submitted;
  239. }
  240. /**
  241. * Returns the name by which the button is identified in forms.
  242. *
  243. * @return string The name of the button.
  244. */
  245. public function getName()
  246. {
  247. return $this->config->getName();
  248. }
  249. /**
  250. * Unsupported method.
  251. *
  252. * @return null Always returns null.
  253. */
  254. public function getPropertyPath()
  255. {
  256. return null;
  257. }
  258. /**
  259. * Unsupported method.
  260. *
  261. * @param FormError $error
  262. *
  263. * @throws BadMethodCallException
  264. */
  265. public function addError(FormError $error)
  266. {
  267. throw new BadMethodCallException('Buttons cannot have errors.');
  268. }
  269. /**
  270. * Unsupported method.
  271. *
  272. * @return Boolean Always returns true.
  273. */
  274. public function isValid()
  275. {
  276. return true;
  277. }
  278. /**
  279. * Unsupported method.
  280. *
  281. * @return Boolean Always returns false.
  282. */
  283. public function isRequired()
  284. {
  285. return false;
  286. }
  287. /**
  288. * {@inheritdoc}
  289. */
  290. public function isDisabled()
  291. {
  292. return $this->config->getDisabled();
  293. }
  294. /**
  295. * Unsupported method.
  296. *
  297. * @return Boolean Always returns true.
  298. */
  299. public function isEmpty()
  300. {
  301. return true;
  302. }
  303. /**
  304. * Unsupported method.
  305. *
  306. * @return Boolean Always returns true.
  307. */
  308. public function isSynchronized()
  309. {
  310. return true;
  311. }
  312. /**
  313. * Unsupported method.
  314. *
  315. * @throws BadMethodCallException
  316. */
  317. public function initialize()
  318. {
  319. throw new BadMethodCallException('Buttons cannot be initialized. Call initialize() on the root form instead.');
  320. }
  321. /**
  322. * Unsupported method.
  323. *
  324. * @param mixed $request
  325. *
  326. * @throws BadMethodCallException
  327. */
  328. public function handleRequest($request = null)
  329. {
  330. throw new BadMethodCallException('Buttons cannot handle requests. Call handleRequest() on the root form instead.');
  331. }
  332. /**
  333. * Submits data to the button.
  334. *
  335. * @param null|string $submittedData The data.
  336. * @param Boolean $clearMissing Not used.
  337. *
  338. * @return Button The button instance
  339. *
  340. * @throws Exception\AlreadySubmittedException If the button has already been submitted.
  341. */
  342. public function submit($submittedData, $clearMissing = true)
  343. {
  344. if ($this->submitted) {
  345. throw new AlreadySubmittedException('A form can only be submitted once');
  346. }
  347. $this->submitted = true;
  348. return $this;
  349. }
  350. /**
  351. * {@inheritdoc}
  352. */
  353. public function getRoot()
  354. {
  355. return $this->parent ? $this->parent->getRoot() : $this;
  356. }
  357. /**
  358. * {@inheritdoc}
  359. */
  360. public function isRoot()
  361. {
  362. return null === $this->parent;
  363. }
  364. /**
  365. * {@inheritdoc}
  366. */
  367. public function createView(FormView $parent = null)
  368. {
  369. if (null === $parent && $this->parent) {
  370. $parent = $this->parent->createView();
  371. }
  372. return $this->config->getType()->createView($this, $parent);
  373. }
  374. /**
  375. * Unsupported method.
  376. *
  377. * @return integer Always returns 0.
  378. */
  379. public function count()
  380. {
  381. return 0;
  382. }
  383. /**
  384. * Unsupported method.
  385. *
  386. * @return \EmptyIterator Always returns an empty iterator.
  387. */
  388. public function getIterator()
  389. {
  390. return new \EmptyIterator();
  391. }
  392. }