Button.php 8.5 KB

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