Skip to content

Commit

Permalink
fixed an issue with the form helper where the value zero would not di…
Browse files Browse the repository at this point in the history
…splay (and a few small edits to pass lithium_qa)
  • Loading branch information
rmarscher committed May 30, 2012
1 parent 9511c05 commit d646c1f
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 7 deletions.
10 changes: 7 additions & 3 deletions template/helper/Form.php
Expand Up @@ -344,9 +344,9 @@ public function end() {
/** /**
* Returns the entity that the `Form` helper is currently bound to. * Returns the entity that the `Form` helper is currently bound to.
* *
* @see lithium\template\helper\Form::$_binding
* @param string $name If specified, match this field name against the list of bindings * @param string $name If specified, match this field name against the list of bindings
* @param string $key If $name specified, where to store relevant $_binding key * @param string $key If $name specified, where to store relevant $_binding key
* @see lithium\template\helper\Form::$_binding
* @return object Returns an object, usually an instance of `lithium\data\Entity`. * @return object Returns an object, usually an instance of `lithium\data\Entity`.
*/ */
public function binding($name = null) { public function binding($name = null) {
Expand Down Expand Up @@ -786,10 +786,14 @@ protected function _defaults($method, $name, $options) {
(!isset($options['value']) || $options['value'] === null) && (!isset($options['value']) || $options['value'] === null) &&
$name && $value = $this->binding($name)->data $name && $value = $this->binding($name)->data
); );
if ($hasValue) { $isZero = (isset($value) && ($value === 0 || $value === "0"));
if ($hasValue || $isZero) {
$options['value'] = $value; $options['value'] = $value;
} }
if (isset($options['default']) && empty($options['value'])) { if (isset($options['value']) && !$isZero) {
$isZero = ($options['value'] === 0 || $options['value'] === "0");
}
if (isset($options['default']) && empty($options['value']) && !$isZero) {
$options['value'] = $options['default']; $options['value'] = $options['default'];
} }
unset($options['default']); unset($options['default']);
Expand Down
27 changes: 23 additions & 4 deletions tests/cases/template/helper/FormTest.php
Expand Up @@ -173,7 +173,9 @@ public function testFormDataBinding() {
'id' => '5', 'id' => '5',
'author_id' => '2', 'author_id' => '2',
'title' => 'This is a saved post', 'title' => 'This is a saved post',
'body' => 'This is the body of the saved post' 'body' => 'This is the body of the saved post',
'zeroInt' => 0,
'zeroString' => "0"
))); )));


$result = $this->form->create($record); $result = $this->form->create($record);
Expand All @@ -187,6 +189,18 @@ public function testFormDataBinding() {
'value' => 'This is a saved post', 'id' => 'MockFormPostTitle' 'value' => 'This is a saved post', 'id' => 'MockFormPostTitle'
))); )));



$result = $this->form->text('zeroInt');
$this->assertTags($result, array('input' => array(
'type' => 'text', 'name' => 'zeroInt',
'value' => '0', 'id' => 'MockFormPostZeroInt'
)));
$result = $this->form->text('zeroString');
$this->assertTags($result, array('input' => array(
'type' => 'text', 'name' => 'zeroString',
'value' => '0', 'id' => 'MockFormPostZeroString'
)));

$this->assertEqual('</form>', $this->form->end()); $this->assertEqual('</form>', $this->form->end());


$this->assertTags($this->form->text('title'), array('input' => array( $this->assertTags($this->form->text('title'), array('input' => array(
Expand Down Expand Up @@ -382,12 +396,16 @@ public function testCheckboxGeneration() {
)) ))
)); ));


$document = new Document(array('model' => $this->_model, 'data' => array('subdocument' => array('foo' => true)))); $document = new Document(array('model' => $this->_model, 'data' =>
array('subdocument' => array('foo' => true))
));
$this->form->create($document); $this->form->create($document);


$result = $this->form->checkbox('subdocument.foo'); $result = $this->form->checkbox('subdocument.foo');
$this->assertTags($result, array( $this->assertTags($result, array(
array('input' => array('type' => 'hidden', 'value' => '', 'name' => 'subdocument[foo]')), array('input' => array(
'type' => 'hidden', 'value' => '', 'name' => 'subdocument[foo]')
),
array('input' => array( array('input' => array(
'type' => 'checkbox', 'value' => '1', 'name' => 'subdocument[foo]', 'type' => 'checkbox', 'value' => '1', 'name' => 'subdocument[foo]',
'checked' => 'checked', 'id' => 'MockFormPostSubdocumentFoo' 'checked' => 'checked', 'id' => 'MockFormPostSubdocumentFoo'
Expand Down Expand Up @@ -614,7 +632,8 @@ public function testSelectGeneration() {


/** /**
* When trying to determine which option of a select box should be selected, we should be * When trying to determine which option of a select box should be selected, we should be
* int/string agnostic because it all looks the same in HTML. * integer/string agnostic because it all looks the same in HTML.
*
*/ */
public function testSelectTypeAgnosticism() { public function testSelectTypeAgnosticism() {
$taglist = array( $taglist = array(
Expand Down

0 comments on commit d646c1f

Please sign in to comment.