Skip to content

Commit

Permalink
Adding a default value for checkbox field
Browse files Browse the repository at this point in the history
  • Loading branch information
chdemko committed Feb 23, 2012
1 parent cdba6ce commit c7ecf93
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
5 changes: 3 additions & 2 deletions libraries/joomla/form/fields/checkbox.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,13 @@ protected function getInput()
// Initialize some field attributes.
$class = $this->element['class'] ? ' class="' . (string) $this->element['class'] . '"' : '';
$disabled = ((string) $this->element['disabled'] == 'true') ? ' disabled="disabled"' : '';
$checked = ((string) $this->element['value'] == $this->value) ? ' checked="checked"' : '';
$value = $this->element['value'] ? (string) $this->element['value'] : '1';
$checked = ($value == $this->value) ? ' checked="checked"' : '';

// Initialize JavaScript field attributes.
$onclick = $this->element['onclick'] ? ' onclick="' . (string) $this->element['onclick'] . '"' : '';

return '<input type="checkbox" name="' . $this->name . '" id="' . $this->id . '"' . ' value="'
. htmlspecialchars((string) $this->element['value'], ENT_COMPAT, 'UTF-8') . '"' . $class . $checked . $disabled . $onclick . '/>';
. htmlspecialchars($value, ENT_COMPAT, 'UTF-8') . '"' . $class . $checked . $disabled . $onclick . ' />';
}
}
8 changes: 7 additions & 1 deletion tests/suite/joomla/form/fields/JFormFieldCheckboxTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public function testGetInput()
$form = new JFormInspector('form1');

$this->assertThat(
$form->load('<form><field name="checkbox" type="checkbox"><option value="all">All</option><option value="none">None</option><option value="something">Something</option><item value="fake">Fake</item></field></form>'),
$form->load('<form><field name="checkbox" type="checkbox" /></form>'),
$this->isTrue(),
'Line:'.__LINE__.' XML string should load successfully.'
);
Expand All @@ -59,5 +59,11 @@ public function testGetInput()
$this->greaterThan(0),
'Line:'.__LINE__.' The getInput method should return something without error.'
);

$this->assertThat(
$field->input,
$this->equalTo('<input type="checkbox" name="checkbox" id="checkbox" value="1" />'),
'Line:'.__LINE__.' The getInput method should return something without error.'
);
}
}

0 comments on commit c7ecf93

Please sign in to comment.