Skip to content

Commit

Permalink
BUGFIX Ensure that CheckboxField always returns 1 from the form reque…
Browse files Browse the repository at this point in the history
…st data instead of "on" - this was because the value attribute didn't exist on the <input> tag

MINOR Added tests for checking saveInto() behaviour on CheckboxFieldTest


git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/branches/2.3@75049 467b73ca-7a2a-4603-9d3b-597d59a354a9
  • Loading branch information
Sean Harvey authored and Sam Minnee committed Feb 2, 2011
1 parent c272082 commit bc443b1
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
1 change: 1 addition & 0 deletions forms/CheckboxField.php
Expand Up @@ -26,6 +26,7 @@ function Field() {
'class' => ($this->extraClass() ? $this->extraClass() : ''),
'id' => $this->id(),
'name' => $this->Name(),
'value' => 1,
'checked' => $this->value ? 'checked' : '',
'tabindex' => $this->getTabIndex()
);
Expand Down
55 changes: 55 additions & 0 deletions tests/forms/CheckboxFieldTest.php
Expand Up @@ -64,5 +64,60 @@ function testFieldValueWithoutSettingValue() {
$this->assertEquals($field->Value(), 0, 'Value() returns a 0');
}

function testSavingChecked() {
/* Create a new test data record */
$article = new CheckboxFieldTest_Article();

/* Create a field, with a value of 1 */
$field = new CheckboxField('IsChecked', 'Checked', 1);

/* Save the field into our Article object */
$field->saveInto($article);

/* Write the record to the test database */
$article->write();

/* Check that IsChecked column contains a 1 */
$this->assertEquals(
DB::query("SELECT IsChecked FROM CheckboxFieldTest_Article")->value(),
1,
'We have a 1 set in the database, because the field saved into as a 1'
);

/* Delete the record we tested */
$article->delete();
}

function testSavingUnchecked() {
/* Create a new test data record */
$article = new CheckboxFieldTest_Article();

/* Create a field, with no value */
$field = new CheckboxField('IsChecked', 'Checked');

/* Save the field into our Article object */
$field->saveInto($article);

/* Write the record to the test database */
$article->write();

/* Check that IsChecked column contains a 0 */
$this->assertEquals(
DB::query("SELECT IsChecked FROM CheckboxFieldTest_Article")->value(),
0,
'We have a 0 set in the database, because the field saved into as a 0'
);

/* Delete the record we tested */
$article->delete();
}

}
class CheckboxFieldTest_Article extends DataObject implements TestOnly {

public static $db = array(
'IsChecked' => 'Boolean'
);

}
?>

0 comments on commit bc443b1

Please sign in to comment.