bug #10926 [DomCrawler] Fixed the initial state for options without value attribute (stof)
This PR was merged into the 2.3 branch.
Discussion
----------
[DomCrawler] Fixed the initial state for options without value attribute
| Q | A
| ------------- | ---
| Bug fix? | yes
| New feature? | no
| BC breaks? | no
| Deprecations? | no
| Tests pass? | yes
| Fixed tickets | -
| License | MIT
| Doc PR | n/a
Commits
-------
78cff96
[DomCrawler] Fixed the initial state for options without value attribute
This commit is contained in:
commit
5c91dc1a3a
|
@ -232,22 +232,22 @@ class ChoiceFormField extends FormField
|
||||||
|
|
||||||
$found = false;
|
$found = false;
|
||||||
foreach ($this->xpath->query('descendant::option', $this->node) as $option) {
|
foreach ($this->xpath->query('descendant::option', $this->node) as $option) {
|
||||||
$this->options[] = $this->buildOptionValue($option);
|
$optionValue = $this->buildOptionValue($option);
|
||||||
|
$this->options[] = $optionValue;
|
||||||
|
|
||||||
if ($option->hasAttribute('selected')) {
|
if ($option->hasAttribute('selected')) {
|
||||||
$found = true;
|
$found = true;
|
||||||
if ($this->multiple) {
|
if ($this->multiple) {
|
||||||
$this->value[] = $option->getAttribute('value');
|
$this->value[] = $optionValue['value'];
|
||||||
} else {
|
} else {
|
||||||
$this->value = $option->getAttribute('value');
|
$this->value = $optionValue['value'];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// if no option is selected and if it is a simple select box, take the first option as the value
|
// if no option is selected and if it is a simple select box, take the first option as the value
|
||||||
$option = $this->xpath->query('descendant::option', $this->node)->item(0);
|
if (!$found && !$this->multiple && !empty($this->options)) {
|
||||||
if (!$found && !$this->multiple && $option instanceof \DOMElement) {
|
$this->value = $this->options[0]['value'];
|
||||||
$this->value = $option->getAttribute('value');
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -319,6 +319,11 @@ class ChoiceFormFieldTest extends FormFieldTestCase
|
||||||
{
|
{
|
||||||
$node = $this->createSelectNodeWithEmptyOption(array('foo' => false, 'bar' => false));
|
$node = $this->createSelectNodeWithEmptyOption(array('foo' => false, 'bar' => false));
|
||||||
$field = new ChoiceFormField($node);
|
$field = new ChoiceFormField($node);
|
||||||
|
$this->assertEquals('foo', $field->getValue());
|
||||||
|
|
||||||
|
$node = $this->createSelectNodeWithEmptyOption(array('foo' => false, 'bar' => true));
|
||||||
|
$field = new ChoiceFormField($node);
|
||||||
|
$this->assertEquals('bar', $field->getValue());
|
||||||
$field->select('foo');
|
$field->select('foo');
|
||||||
$this->assertEquals('foo', $field->getValue(), '->select() changes the selected option');
|
$this->assertEquals('foo', $field->getValue(), '->select() changes the selected option');
|
||||||
}
|
}
|
||||||
|
|
Reference in New Issue