8000 bug #18460 [DomCrawler] Fix select option with empty value (Matt Wells) · symfony/symfony@abf4f67 · GitHub
[go: up one dir, main page]

Skip to content

Commit abf4f67

Browse files
committed
bug #18460 [DomCrawler] Fix select option with empty value (Matt Wells)
This PR was merged into the 2.3 branch. Discussion ---------- [DomCrawler] Fix select option with empty value | Q | A | ------------- | --- | Branch? | 2.3+ | Bug fix? | yes | New feature? | no | BC breaks? | no? | Deprecations? | no | Tests pass? | yes | Fixed tickets | n/a | License | MIT | Doc PR | n/a While using the Laravel's testing suite which makes use of the Symfony DOM Crawler (v3.0.2). I have been populating a form with a select which has a value which can be an empty value. For example, with this select you can choose your gender or leave it empty if you don't want to specify: ```html <select name="gender"> <option selected></option> <option>Female</option> <option>Male</option> </select> ``` When the `DomCrawler\Field::getValue()` is called I was expect to get the value `''` however I was actually getting `'on'`. This is caused by the [DomCrawler\Field::buildOptionValue()](https://github.com/symfony/symfony/blob/master/src/Symfony/Component/DomCrawler/Field/ChoiceFormField.php#L262-L271) sets the default value to 'on' when there is no value which makes sense for ratios and checkboxes but not for select. I have tracked this bug back to v2.3 but it is still present in v3, however, the default value was changed from '1' to 'on' in v2.5 which means that this patch will conflict when merging up the maintained versions. Commits ------- 58276a2 Fix Dom Crawler select option with empty value
2 parents e251e36 + 58276a2 commit abf4f67

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,8 @@ private function buildOptionValue($node)
259259
{
260260
$option = array();
261261

262-
$defaultValue = (isset($node->nodeValue) && !empty($node->nodeValue)) ? $node->nodeValue : '1';
262+
$defaultDefaultValue = 'select' === $this->node->nodeName ? '' : '1';
263+
$defaultValue = (isset($node->nodeValue) && !empty($node->nodeValue)) ? $node->nodeValue : $defaultDefaultValue;
263264
$option['value'] = $node->hasAttribute('value') ? $node->getAttribute('value') : $defaultValue;
264265
$option['disabled'] = $node->hasAttribute('disabled');
265266

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,14 @@ public function testOptionWithNoValue()
336336
$this->assertEquals('foo', $field->getValue(), '->select() changes the selected option');
337337
}
338338

339+
public function testSelectWithEmptyValue()
340+
{
341+
$node = $this->createSelectNodeWithEmptyOption(array('' => true, 'Female' => false, 'Male' => false));
342+
$field = new ChoiceFormField($node);
343+
344+
$this->assertSame('', $field->getValue());
345+
}
346+
339347
protected function createSelectNode($options, $attributes = array(), $selectedAttrText = 'selected')
340348
{
341349
$document = new \DOMDocument();

0 commit comments

Comments
 (0)
0