ChoiceFormField.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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 a HTML select tag, or a 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 $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. * @param \DOMElement $node
  141. *
  142. * @throws \LogicException When choice provided is not multiple nor radio
  143. *
  144. * @internal
  145. */
  146. public function addChoice(\DOMElement $node)
  147. {
  148. if (!$this->multiple && 'radio' !== $this->type) {
  149. throw new \LogicException(sprintf('Unable to add a choice for "%s" as it is not multiple or is not a radio button.', $this->name));
  150. }
  151. $option = $this->buildOptionValue($node);
  152. $this->options[] = $option;
  153. if ($node->hasAttribute('checked')) {
  154. $this->value = $option['value'];
  155. }
  156. }
  157. /**
  158. * Returns the type of the choice field (radio, select, or checkbox).
  159. *
  160. * @return string The type
  161. */
  162. public function getType()
  163. {
  164. return $this->type;
  165. }
  166. /**
  167. * Returns true if the field accepts multiple values.
  168. *
  169. * @return bool true if the field accepts multiple values, false otherwise
  170. */
  171. public function isMultiple()
  172. {
  173. return $this->multiple;
  174. }
  175. /**
  176. * Initializes the form field.
  177. *
  178. * @throws \LogicException When node type is incorrect
  179. */
  180. protected function initialize()
  181. {
  182. if ('input' !== $this->node->nodeName && 'select' !== $this->node->nodeName) {
  183. throw new \LogicException(sprintf('A ChoiceFormField can only be created from an input or select tag (%s given).', $this->node->nodeName));
  184. }
  185. if ('input' === $this->node->nodeName && 'checkbox' !== strtolower($this->node->getAttribute('type')) && 'radio' !== strtolower($this->node->getAttribute('type'))) {
  186. 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')));
  187. }
  188. $this->value = null;
  189. $this->options = [];
  190. $this->multiple = false;
  191. if ('input' == $this->node->nodeName) {
  192. $this->type = strtolower($this->node->getAttribute('type'));
  193. $optionValue = $this->buildOptionValue($this->node);
  194. $this->options[] = $optionValue;
  195. if ($this->node->hasAttribute('checked')) {
  196. $this->value = $optionValue['value'];
  197. }
  198. } else {
  199. $this->type = 'select';
  200. if ($this->node->hasAttribute('multiple')) {
  201. $this->multiple = true;
  202. $this->value = [];
  203. $this->name = str_replace('[]', '', $this->name);
  204. }
  205. $found = false;
  206. foreach ($this->xpath->query('descendant::option', $this->node) as $option) {
  207. $optionValue = $this->buildOptionValue($option);
  208. $this->options[] = $optionValue;
  209. if ($option->hasAttribute('selected')) {
  210. $found = true;
  211. if ($this->multiple) {
  212. $this->value[] = $optionValue['value'];
  213. } else {
  214. $this->value = $optionValue['value'];
  215. }
  216. }
  217. }
  218. // if no option is selected and if it is a simple select box, take the first option as the value
  219. if (!$found && !$this->multiple && !empty($this->options)) {
  220. $this->value = $this->options[0]['value'];
  221. }
  222. }
  223. }
  224. /**
  225. * Returns option value with associated disabled flag.
  226. *
  227. * @param \DOMElement $node
  228. *
  229. * @return array
  230. */
  231. private function buildOptionValue(\DOMElement $node)
  232. {
  233. $option = [];
  234. $defaultDefaultValue = 'select' === $this->node->nodeName ? '' : 'on';
  235. $defaultValue = (isset($node->nodeValue) && !empty($node->nodeValue)) ? $node->nodeValue : $defaultDefaultValue;
  236. $option['value'] = $node->hasAttribute('value') ? $node->getAttribute('value') : $defaultValue;
  237. $option['disabled'] = $node->hasAttribute('disabled');
  238. return $option;
  239. }
  240. /**
  241. * Checks whether given value is in the existing options.
  242. *
  243. * @param string $optionValue
  244. * @param array $options
  245. *
  246. * @return bool
  247. */
  248. public function containsOption($optionValue, $options)
  249. {
  250. if ($this->validationDisabled) {
  251. return true;
  252. }
  253. foreach ($options as $option) {
  254. if ($option['value'] == $optionValue) {
  255. return true;
  256. }
  257. }
  258. return false;
  259. }
  260. /**
  261. * Returns list of available field options.
  262. *
  263. * @return array
  264. */
  265. public function availableOptionValues()
  266. {
  267. $values = [];
  268. foreach ($this->options as $option) {
  269. $values[] = $option['value'];
  270. }
  271. return $values;
  272. }
  273. /**
  274. * Disables the internal validation of the field.
  275. *
  276. * @return self
  277. */
  278. public function disableValidation()
  279. {
  280. $this->validationDisabled = true;
  281. return $this;
  282. }
  283. }