Skip to content

Commit 5c91dc1

Browse files
committed
bug symfony#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
2 parents e4a28dd + 78cff96 commit 5c91dc1

File tree

2 files changed

+11
-6
lines changed

2 files changed

+11
-6
lines changed

src/Symfony/Component/DomCrawler/Field/ChoiceFormField.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -232,22 +232,22 @@ protected function initialize()
232232

233233
$found = false;
234234
foreach ($this->xpath->query('descendant::option', $this->node) as $option) {
235-
$this->options[] = $this->buildOptionValue($option);
235+
$optionValue = $this->buildOptionValue($option);
236+
$this->options[] = $optionValue;
236237

237238
if ($option->hasAttribute('selected')) {
238239
$found = true;
239240
if ($this->multiple) {
240-
$this->value[] = $option->getAttribute('value');
241+
$this->value[] = $optionValue['value'];
241242
} else {
242-
$this->value = $option->getAttribute('value');
243+
$this->value = $optionValue['value'];
243244
}
244245
}
245246
}
246247

247248
// if no option is selected and if it is a simple select box, take the first option as the value
248-
$option = $this->xpath->query('descendant::option', $this->node)->item(0);
249-
if (!$found && !$this->multiple && $option instanceof \DOMElement) {
250-
$this->value = $option->getAttribute('value');
249+
if (!$found && !$this->multiple && !empty($this->options)) {
250+
$this->value = $this->options[0]['value'];
251251
}
252252
}
253253
}

src/Symfony/Component/DomCrawler/Tests/Field/ChoiceFormFieldTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,11 @@ public function testOptionWithNoValue()
319319
{
320320
$node = $this->createSelectNodeWithEmptyOption(array('foo' => false, 'bar' => false));
321321
$field = new ChoiceFormField($node);
322+
$this->assertEquals('foo', $field->getValue());
323+
324+
$node = $this->createSelectNodeWithEmptyOption(array('foo' => false, 'bar' => true));
325+
$field = new ChoiceFormField($node);
326+
$this->assertEquals('bar', $field->getValue());
322327
$field->select('foo');
323328
$this->assertEquals('foo', $field->getValue(), '->select() changes the selected option');
324329
}

0 commit comments

Comments
 (0)