diff --git a/forms/DropdownField.php b/forms/DropdownField.php index 5115f28fd..60b66df5e 100644 --- a/forms/DropdownField.php +++ b/forms/DropdownField.php @@ -164,7 +164,12 @@ class DropdownField extends FormField { $selected = true; } else { // check against value, fallback to a type check comparison when !value - $selected = ($value) ? $value == $this->value : $value === $this->value; + if($value) { + $selected = ($value == $this->value); + } else { + $selected = ($value === $this->value) || (((string) $value) === ((string) $this->value)); + } + $this->isSelected = $selected; } diff --git a/tests/forms/DropdownFieldTest.php b/tests/forms/DropdownFieldTest.php index ad0f669d2..1167cc5f4 100644 --- a/tests/forms/DropdownFieldTest.php +++ b/tests/forms/DropdownFieldTest.php @@ -79,7 +79,52 @@ class DropdownFieldTest extends SapphireTest { ) ); } - + + public function testStringZeroValueSelectedOptionBehaviour() { + $field = new DropdownField('Field', null, array( + '-1' => 'some negative', + '0' => 'none', + '1' => 'one', + '2+' => 'two or more' + ), '0'); + + $selectedOptions = $this->findSelectedOptionElements($field->Field()); + $this->assertEquals((string) $selectedOptions[0], 'none', 'The selected option is "none"'); + + $field = new DropdownField('Field', null, array( + '-1' => 'some negative', + '0' => 'none', + '1' => 'one', + '2+' => 'two or more' + ), 0); + + $selectedOptions = $this->findSelectedOptionElements($field->Field()); + $this->assertEquals((string) $selectedOptions[0], 'none', 'The selected option is "none"'); + } + + public function testStringOneValueSelectedOptionBehaviour() { + $field = new DropdownField('Field', null, array( + '-1' => 'some negative', + '0' => 'none', + '1' => 'one', + '2+' => 'two or more' + ), '1'); + + + $selectedOptions = $this->findSelectedOptionElements($field->Field()); + $this->assertEquals((string) $selectedOptions[0], 'one', 'The selected option is "one"'); + + $field = new DropdownField('Field', null, array( + '-1' => 'some negative', + '0' => 'none', + '1' => 'one', + '2+' => 'two or more' + ), 1); + + $selectedOptions = $this->findSelectedOptionElements($field->Field()); + $this->assertEquals((string) $selectedOptions[0], 'one', 'The selected option is "one"'); + } + public function testNumberOfSelectOptionsAvailable() { /* Create a field with a blank value */ $field = $this->createDropdownField('(Any)');