ChoiceFormField.php 8.9 KB

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