4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\DomCrawler\Field;
15 * ChoiceFormField represents a choice form field.
17 * It is constructed from a HTML select tag, or a HTML checkbox, or radio inputs.
19 * @author Fabien Potencier <fabien@symfony.com>
21 class ChoiceFormField extends FormField
38 private $validationDisabled = false;
41 * Returns true if the field should be included in the submitted values.
43 * @return bool true if the field should be included in the submitted values, false otherwise
45 public function hasValue()
47 // don't send a value for unchecked checkboxes
48 if (in_array($this->type, array('checkbox', 'radio')) && null === $this->value) {
56 * Check if the current selected option is disabled.
60 public function isDisabled()
62 if (parent::isDisabled() && 'select' === $this->type) {
66 foreach ($this->options as $option) {
67 if ($option['value'] == $this->value && $option['disabled']) {
76 * Sets the value of the field.
78 * @param string $value The value of the field
80 public function select($value)
82 $this->setValue($value);
88 * @throws \LogicException When the type provided is not correct
90 public function tick()
92 if ('checkbox' !== $this->type) {
93 throw new \LogicException(sprintf('You cannot tick "%s" as it is not a checkbox (%s).', $this->name, $this->type));
96 $this->setValue(true);
100 * Unticks a checkbox.
102 * @throws \LogicException When the type provided is not correct
104 public function untick()
106 if ('checkbox' !== $this->type) {
107 throw new \LogicException(sprintf('You cannot untick "%s" as it is not a checkbox (%s).', $this->name, $this->type));
110 $this->setValue(false);
114 * Sets the value of the field.
116 * @param string|array $value The value of the field
118 * @throws \InvalidArgumentException When value type provided is not correct
120 public function setValue($value)
122 if ('checkbox' === $this->type && false === $value) {
125 } elseif ('checkbox' === $this->type && true === $value) {
127 $this->value = $this->options[0]['value'];
129 if (is_array($value)) {
130 if (!$this->multiple) {
131 throw new \InvalidArgumentException(sprintf('The value for "%s" cannot be an array.', $this->name));
134 foreach ($value as $v) {
135 if (!$this->containsOption($v, $this->options)) {
136 throw new \InvalidArgumentException(sprintf('Input "%s" cannot take "%s" as a value (possible values: %s).', $this->name, $v, implode(', ', $this->availableOptionValues())));
139 } elseif (!$this->containsOption($value, $this->options)) {
140 throw new \InvalidArgumentException(sprintf('Input "%s" cannot take "%s" as a value (possible values: %s).', $this->name, $value, implode(', ', $this->availableOptionValues())));
143 if ($this->multiple) {
144 $value = (array) $value;
147 if (is_array($value)) {
148 $this->value = $value;
150 parent::setValue($value);
156 * Adds a choice to the current ones.
158 * @param \DOMElement $node
160 * @throws \LogicException When choice provided is not multiple nor radio
164 public function addChoice(\DOMElement $node)
166 if (!$this->multiple && 'radio' !== $this->type) {
167 throw new \LogicException(sprintf('Unable to add a choice for "%s" as it is not multiple or is not a radio button.', $this->name));
170 $option = $this->buildOptionValue($node);
171 $this->options[] = $option;
173 if ($node->hasAttribute('checked')) {
174 $this->value = $option['value'];
179 * Returns the type of the choice field (radio, select, or checkbox).
181 * @return string The type
183 public function getType()
189 * Returns true if the field accepts multiple values.
191 * @return bool true if the field accepts multiple values, false otherwise
193 public function isMultiple()
195 return $this->multiple;
199 * Initializes the form field.
201 * @throws \LogicException When node type is incorrect
203 protected function initialize()
205 if ('input' !== $this->node->nodeName && 'select' !== $this->node->nodeName) {
206 throw new \LogicException(sprintf('A ChoiceFormField can only be created from an input or select tag (%s given).', $this->node->nodeName));
209 if ('input' === $this->node->nodeName && 'checkbox' !== strtolower($this->node->getAttribute('type')) && 'radio' !== strtolower($this->node->getAttribute('type'))) {
210 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')));
214 $this->options = array();
215 $this->multiple = false;
217 if ('input' == $this->node->nodeName) {
218 $this->type = strtolower($this->node->getAttribute('type'));
219 $optionValue = $this->buildOptionValue($this->node);
220 $this->options[] = $optionValue;
222 if ($this->node->hasAttribute('checked')) {
223 $this->value = $optionValue['value'];
226 $this->type = 'select';
227 if ($this->node->hasAttribute('multiple')) {
228 $this->multiple = true;
229 $this->value = array();
230 $this->name = str_replace('[]', '', $this->name);
234 foreach ($this->xpath->query('descendant::option', $this->node) as $option) {
235 $optionValue = $this->buildOptionValue($option);
236 $this->options[] = $optionValue;
238 if ($option->hasAttribute('selected')) {
240 if ($this->multiple) {
241 $this->value[] = $optionValue['value'];
243 $this->value = $optionValue['value'];
248 // if no option is selected and if it is a simple select box, take the first option as the value
249 if (!$found && !$this->multiple && !empty($this->options)) {
250 $this->value = $this->options[0]['value'];
256 * Returns option value with associated disabled flag.
258 * @param \DOMElement $node
262 private function buildOptionValue(\DOMElement $node)
266 $defaultDefaultValue = 'select' === $this->node->nodeName ? '' : 'on';
267 $defaultValue = (isset($node->nodeValue) && !empty($node->nodeValue)) ? $node->nodeValue : $defaultDefaultValue;
268 $option['value'] = $node->hasAttribute('value') ? $node->getAttribute('value') : $defaultValue;
269 $option['disabled'] = $node->hasAttribute('disabled');
275 * Checks whether given value is in the existing options.
277 * @param string $optionValue
278 * @param array $options
282 public function containsOption($optionValue, $options)
284 if ($this->validationDisabled) {
288 foreach ($options as $option) {
289 if ($option['value'] == $optionValue) {
298 * Returns list of available field options.
302 public function availableOptionValues()
306 foreach ($this->options as $option) {
307 $values[] = $option['value'];
314 * Disables the internal validation of the field.
318 public function disableValidation()
320 $this->validationDisabled = true;