From 186b65ea5d8fad1b4654aa77cbd71dc15edcd700 Mon Sep 17 00:00:00 2001 From: Christophe Coevoet Date: Sat, 17 May 2014 22:04:22 +0200 Subject: [PATCH] Changed the default value of checkbox and radio to match the HTML spec When the checkbox or radio input does not have a value attribute, the HTML spec defines that the value should be 'on', not '1'. --- src/Symfony/Component/DomCrawler/CHANGELOG.md | 2 ++ src/Symfony/Component/DomCrawler/Field/ChoiceFormField.php | 2 +- .../DomCrawler/Tests/Field/ChoiceFormFieldTest.php | 6 +++--- 3 files changed, 6 insertions(+), 4 deletions(-) diff --git a/src/Symfony/Component/DomCrawler/CHANGELOG.md b/src/Symfony/Component/DomCrawler/CHANGELOG.md index b60ac0140e..48fd323f82 100644 --- a/src/Symfony/Component/DomCrawler/CHANGELOG.md +++ b/src/Symfony/Component/DomCrawler/CHANGELOG.md @@ -4,6 +4,8 @@ CHANGELOG 2.5.0 ----- +* [BC BREAK] The default value for checkbox and radio inputs without a value attribute have changed + from '1' to 'on' to match the HTML specification. * [BC BREAK] The typehints on the `Link`, `Form` and `FormField` classes have been changed from `\DOMNode` to `DOMElement`. Using any other type of `DOMNode` was triggering fatal errors in previous versions. Code extending these classes will need to update the typehints when overwriting these methods. diff --git a/src/Symfony/Component/DomCrawler/Field/ChoiceFormField.php b/src/Symfony/Component/DomCrawler/Field/ChoiceFormField.php index 8575e7216f..7ccc4fa15b 100644 --- a/src/Symfony/Component/DomCrawler/Field/ChoiceFormField.php +++ b/src/Symfony/Component/DomCrawler/Field/ChoiceFormField.php @@ -267,7 +267,7 @@ class ChoiceFormField extends FormField { $option = array(); - $defaultValue = (isset($node->nodeValue) && !empty($node->nodeValue)) ? $node->nodeValue : '1'; + $defaultValue = (isset($node->nodeValue) && !empty($node->nodeValue)) ? $node->nodeValue : 'on'; $option['value'] = $node->hasAttribute('value') ? $node->getAttribute('value') : $defaultValue; $option['disabled'] = ($node->hasAttribute('disabled') && $node->getAttribute('disabled') == 'disabled'); diff --git a/src/Symfony/Component/DomCrawler/Tests/Field/ChoiceFormFieldTest.php b/src/Symfony/Component/DomCrawler/Tests/Field/ChoiceFormFieldTest.php index f0b086816e..09deb21370 100644 --- a/src/Symfony/Component/DomCrawler/Tests/Field/ChoiceFormFieldTest.php +++ b/src/Symfony/Component/DomCrawler/Tests/Field/ChoiceFormFieldTest.php @@ -201,7 +201,7 @@ class ChoiceFormFieldTest extends FormFieldTestCase $field = new ChoiceFormField($node); $this->assertTrue($field->hasValue(), '->hasValue() returns true when the checkbox is checked'); - $this->assertEquals('1', $field->getValue(), '->getValue() returns 1 if the checkbox is checked and has no value attribute'); + $this->assertEquals('on', $field->getValue(), '->getValue() returns 1 if the checkbox is checked and has no value attribute'); $node = $this->createNode('input', '', array('type' => 'checkbox', 'name' => 'name', 'checked' => 'checked', 'value' => 'foo')); $field = new ChoiceFormField($node); @@ -240,7 +240,7 @@ class ChoiceFormFieldTest extends FormFieldTestCase $node = $this->createNode('input', '', array('type' => 'checkbox', 'name' => 'name')); $field = new ChoiceFormField($node); $field->tick(); - $this->assertEquals(1, $field->getValue(), '->tick() ticks checkboxes'); + $this->assertEquals('on', $field->getValue(), '->tick() ticks checkboxes'); } public function testUntick() @@ -266,7 +266,7 @@ class ChoiceFormFieldTest extends FormFieldTestCase $node = $this->createNode('input', '', array('type' => 'checkbox', 'name' => 'name', 'checked' => 'checked')); $field = new ChoiceFormField($node); $field->select(true); - $this->assertEquals(1, $field->getValue(), '->select() changes the value of the field'); + $this->assertEquals('on', $field->getValue(), '->select() changes the value of the field'); $field->select(false); $this->assertNull($field->getValue(), '->select() changes the value of the field');