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:
Fabien Potencier 2014-05-17 13:54:50 +02:00
commit 5c91dc1a3a
2 changed files with 11 additions and 6 deletions

View File

@ -232,22 +232,22 @@ class ChoiceFormField extends FormField
$found = false;
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')) {
$found = true;
if ($this->multiple) {
$this->value[] = $option->getAttribute('value');
$this->value[] = $optionValue['value'];
} 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
$option = $this->xpath->query('descendant::option', $this->node)->item(0);
if (!$found && !$this->multiple && $option instanceof \DOMElement) {
$this->value = $option->getAttribute('value');
if (!$found && !$this->multiple && !empty($this->options)) {
$this->value = $this->options[0]['value'];
}
}
}

View File

@ -319,6 +319,11 @@ class ChoiceFormFieldTest extends FormFieldTestCase
{
$node = $this->createSelectNodeWithEmptyOption(array('foo' => false, 'bar' => false));
$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');
$this->assertEquals('foo', $field->getValue(), '->select() changes the selected option');
}