Skip to content

Commit 78cff96

Browse files
committed
[DomCrawler] Fixed the initial state for options without value attribute
1 parent 0762bae commit 78cff96

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->getAttribute('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
@@ -280,6 +280,11 @@ public function testOptionWithNoValue()
280280
{
281281
$node = $this->createSelectNodeWithEmptyOption(array('foo' => false, 'bar' => false));
282282
$field = new ChoiceFormField($node);
283+
$this->assertEquals('foo', $field->getValue());
284+
285+
$node = $this->createSelectNodeWithEmptyOption(array('foo' => false, 'bar' => true));
286+
$field = new ChoiceFormField($node);
287+
$this->assertEquals('bar', $field->getValue());
283288
$field->select('foo');
284289
$this->assertEquals('foo', $field->getValue(), '->select() changes the selected option');
285290
}

0 commit comments

Comments
 (0)