ChoiceFormField.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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\DomCrawler\Field;
  11. /**
  12. * ChoiceFormField represents a choice form field.
  13. *
  14. * It is constructed from an HTML select tag, or an HTML checkbox, or radio inputs.
  15. *
  16. * @author Fabien Potencier <fabien@symfony.com>
  17. */
  18. class ChoiceFormField extends FormField
  19. {
  20. /**
  21. * @var string
  22. */
  23. private $type;
  24. /**
  25. * @var bool
  26. */
  27. private $multiple;
  28. /**
  29. * @var array
  30. */
  31. private $options;
  32. /**
  33. * @var bool
  34. */
  35. private $validationDisabled = false;
  36. /**
  37. * Returns true if the field should be included in the submitted values.
  38. *
  39. * @return bool true if the field should be included in the submitted values, false otherwise
  40. */
  41. public function hasValue()
  42. {
  43. // don't send a value for unchecked checkboxes
  44. if (\in_array($this->type, ['checkbox', 'radio']) && null === $this->value) {
  45. return false;
  46. }
  47. return true;
  48. }
  49. /**
  50. * Check if the current selected option is disabled.
  51. *
  52. * @return bool
  53. */
  54. public function isDisabled()
  55. {
  56. if (parent::isDisabled() && 'select' === $this->type) {
  57. return true;
  58. }
  59. foreach ($this->options as $option) {
  60. if ($option['value'] == $this->value && $option['disabled']) {
  61. return true;
  62. }
  63. }
  64. return false;
  65. }
  66. /**
  67. * Sets the value of the field.
  68. *
  69. * @param string|array $value The value of the field
  70. */
  71. public function select($value)
  72. {
  73. $this->setValue($value);
  74. }
  75. /**
  76. * Ticks a checkbox.
  77. *
  78. * @throws \LogicException When the type provided is not correct
  79. */
  80. public function tick()
  81. {
  82. if ('checkbox' !== $this->type) {
  83. throw new \LogicException(sprintf('You cannot tick "%s" as it is not a checkbox (%s).', $this->name, $this->type));
  84. }
  85. $this->setValue(true);
  86. }
  87. /**
  88. * Unticks a checkbox.
  89. *
  90. * @throws \LogicException When the type provided is not correct
  91. */
  92. public function untick()
  93. {
  94. if ('checkbox' !== $this->type) {
  95. throw new \LogicException(sprintf('You cannot untick "%s" as it is not a checkbox (%s).', $this->name, $this->type));
  96. }
  97. $this->setValue(false);
  98. }
  99. /**
  100. * Sets the value of the field.
  101. *
  102. * @param string|array|bool|null $value The value of the field
  103. *
  104. * @throws \InvalidArgumentException When value type provided is not correct
  105. */
  106. public function setValue($value)
  107. {
  108. if ('checkbox' === $this->type && false === $value) {
  109. // uncheck
  110. $this->value = null;
  111. } elseif ('checkbox' === $this->type && true === $value) {
  112. // check
  113. $this->value = $this->options[0]['value'];
  114. } else {
  115. if (\is_array($value)) {
  116. if (!$this->multiple) {
  117. throw new \InvalidArgumentException(sprintf('The value for "%s" cannot be an array.', $this->name));
  118. }
  119. foreach ($value as $v) {
  120. if (!$this->containsOption($v, $this->options)) {
  121. throw new \InvalidArgumentException(sprintf('Input "%s" cannot take "%s" as a value (possible values: "%s").', $this->name, $v, implode('", "', $this->availableOptionValues())));
  122. }
  123. }
  124. } elseif (!$this->containsOption($value, $this->options)) {
  125. throw new \InvalidArgumentException(sprintf('Input "%s" cannot take "%s" as a value (possible values: "%s").', $this->name, $value, implode('", "', $this->availableOptionValues())));
  126. }
  127. if ($this->multiple) {
  128. $value = (array) $value;
  129. }
  130. if (\is_array($value)) {
  131. $this->value = $value;
  132. } else {
  133. parent::setValue($value);
  134. }
  135. }
  136. }
  137. /**
  138. * Adds a choice to the current ones.
  139. *
  140. * @throws \LogicException When choice provided is not multiple nor radio
  141. *
  142. * @internal
  143. */
  144. public function addChoice(\DOMElement $node)
  145. {
  146. if (!$this->multiple && 'radio' !== $this->type) {
  147. throw new \LogicException(sprintf('Unable to add a choice for "%s" as it is not multiple or is not a radio button.', $this->name));
  148. }
  149. $option = $this->buildOptionValue($node);
  150. $this->options[] = $option;
  151. if ($node->hasAttribute('checked')) {
  152. $this->value = $option['value'];
  153. }
  154. }
  155. /**
  156. * Returns the type of the choice field (radio, select, or checkbox).
  157. *
  158. * @return string The type
  159. */
  160. public function getType()
  161. {
  162. return $this->type;
  163. }
  164. /**
  165. * Returns true if the field accepts multiple values.
  166. *
  167. * @return bool true if the field accepts multiple values, false otherwise
  168. */
  169. public function isMultiple()
  170. {
  171. return $this->multiple;
  172. }
  173. /**
  174. * Initializes the form field.
  175. *
  176. * @throws \LogicException When node type is incorrect
  177. */
  178. protected function initialize()
  179. {
  180. if ('input' !== $this->node->nodeName && 'select' !== $this->node->nodeName) {
  181. throw new \LogicException(sprintf('A ChoiceFormField can only be created from an input or select tag (%s given).', $this->node->nodeName));
  182. }
  183. if ('input' === $this->node->nodeName && 'checkbox' !== strtolower($this->node->getAttribute('type')) && 'radio' !== strtolower($this->node->getAttribute('type'))) {
  184. throw new \LogicException(sprintf('A ChoiceFormField can only be created from an input tag with a type of checkbox or radio (given type is "%s").', $this->node->getAttribute('type')));
  185. }
  186. $this->value = null;
  187. $this->options = [];
  188. $this->multiple = false;
  189. if ('input' == $this->node->nodeName) {
  190. $this->type = strtolower($this->node->getAttribute('type'));
  191. $optionValue = $this->buildOptionValue($this->node);
  192. $this->options[] = $optionValue;
  193. if ($this->node->hasAttribute('checked')) {
  194. $this->value = $optionValue['value'];
  195. }
  196. } else {
  197. $this->type = 'select';
  198. if ($this->node->hasAttribute('multiple')) {
  199. $this->multiple = true;
  200. $this->value = [];
  201. $this->name = str_replace('[]', '', $this->name);
  202. }
  203. $found = false;
  204. foreach ($this->xpath->query('descendant::option', $this->node) as $option) {
  205. $optionValue = $this->buildOptionValue($option);
  206. $this->options[] = $optionValue;
  207. if ($option->hasAttribute('selected')) {
  208. $found = true;
  209. if ($this->multiple) {
  210. $this->value[] = $optionValue['value'];
  211. } else {
  212. $this->value = $optionValue['value'];
  213. }
  214. }
  215. }
  216. // if no option is selected and if it is a simple select box, take the first option as the value
  217. if (!$found && !$this->multiple && !empty($this->options)) {
  218. $this->value = $this->options[0]['value'];
  219. }
  220. }
  221. }
  222. /**
  223. * Returns option value with associated disabled flag.
  224. */
  225. private function buildOptionValue(\DOMElement $node): array
  226. {
  227. $option = [];
  228. $defaultDefaultValue = 'select' === $this->node->nodeName ? '' : 'on';
  229. $defaultValue = (isset($node->nodeValue) && !empty($node->nodeValue)) ? $node->nodeValue : $defaultDefaultValue;
  230. $option['value'] = $node->hasAttribute('value') ? $node->getAttribute('value') : $defaultValue;
  231. $option['disabled'] = $node->hasAttribute('disabled');
  232. return $option;
  233. }
  234. /**
  235. * Checks whether given value is in the existing options.
  236. *
  237. * @return bool
  238. */
  239. public function containsOption(string $optionValue, array $options)
  240. {
  241. if ($this->validationDisabled) {
  242. return true;
  243. }
  244. foreach ($options as $option) {
  245. if ($option['value'] == $optionValue) {
  246. return true;
  247. }
  248. }
  249. return false;
  250. }
  251. /**
  252. * Returns list of available field options.
  253. *
  254. * @return array
  255. */
  256. public function availableOptionValues()
  257. {
  258. $values = [];
  259. foreach ($this->options as $option) {
  260. $values[] = $option['value'];
  261. }
  262. return $values;
  263. }
  264. /**
  265. * Disables the internal validation of the field.
  266. *
  267. * @return self
  268. */
  269. public function disableValidation()
  270. {
  271. $this->validationDisabled = true;
  272. return $this;
  273. }
  274. }