Skip to content

Commit

Permalink
Fix issue when providing explicit default value for input.
Browse files Browse the repository at this point in the history
  • Loading branch information
ADmad committed Apr 30, 2016
1 parent 4b9095c commit 35af288
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/View/Form/EntityContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,10 @@ public function val($field, $options = [])
if ($val !== null) {
return $val;
}
if (!$options['schemaDefault']) {
if ($options['default'] !== null
|| !$options['schemaDefault']
|| !$entity->isNew()
) {
return $options['default'];
}
return $this->_schemaDefault($part, $entity);
Expand Down
73 changes: 73 additions & 0 deletions tests/TestCase/View/Helper/FormHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4029,6 +4029,37 @@ public function testTextDefaultValue()
$result = $this->Form->text('Model.field', ['default' => 'default value']);
$expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'value' => 'default value']];
$this->assertHtml($expected, $result);

$Articles = TableRegistry::get('Articles');
$title = $Articles->schema()->column('title');
$Articles->schema()->addColumn(
'title',
['default' => 'default title'] + $title
);

$entity = $Articles->newEntity();
$this->Form->create($entity);

// Get default value from schema
$result = $this->Form->text('title');
$expected = ['input' => ['type' => 'text', 'name' => 'title', 'value' => 'default title']];
$this->assertHtml($expected, $result);

// Don't get value from schema
$result = $this->Form->text('title', ['schemaDefault' => false]);
$expected = ['input' => ['type' => 'text', 'name' => 'title']];
$this->assertHtml($expected, $result);

// Custom default value overrides default value from schema
$result = $this->Form->text('title', ['default' => 'override default']);
$expected = ['input' => ['type' => 'text', 'name' => 'title', 'value' => 'override default']];
$this->assertHtml($expected, $result);

// Default value from schema is used only for new entities.
$entity->isNew(false);
$result = $this->Form->text('title');
$expected = ['input' => ['type' => 'text', 'name' => 'title']];
$this->assertHtml($expected, $result);
}

/**
Expand Down Expand Up @@ -4238,6 +4269,37 @@ public function testRadio()
$this->assertHtml($expected, $result);
}

/**
* Test default value setting on radio() method
*
* @return void
*/
public function testRadioDefaultValue()
{
$Articles = TableRegistry::get('Articles');
$title = $Articles->schema()->column('title');
$Articles->schema()->addColumn(
'title',
['default' => '1'] + $title
);

$this->Form->create($Articles->newEntity());

$result = $this->Form->radio('title', ['option A', 'option B']);
$expected = [
['input' => ['type' => 'hidden', 'name' => 'title', 'value' => '']],
['label' => ['for' => 'title-0']],
['input' => ['type' => 'radio', 'name' => 'title', 'value' => '0', 'id' => 'title-0']],
'option A',
'/label',
['label' => ['for' => 'title-1']],
['input' => ['type' => 'radio', 'name' => 'title', 'value' => '1', 'id' => 'title-1', 'checked' => 'checked']],
'option B',
'/label',
];
$this->assertHtml($expected, $result);
}

/**
* Test that input works with radio types
*
Expand Down Expand Up @@ -5307,6 +5369,17 @@ public function testCheckboxDefaultValue()
$result = $this->Form->checkbox('Model.field', ['default' => false, 'hiddenField' => false]);
$expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1']];
$this->assertHtml($expected, $result);

$Articles = TableRegistry::get('Articles');
$Articles->schema()->addColumn(
'published',
['type' => 'boolean', 'null' => false, 'default' => true]
);

$this->Form->create($Articles->newEntity());
$result = $this->Form->checkbox('published', ['hiddenField' => false]);
$expected = ['input' => ['type' => 'checkbox', 'name' => 'published', 'value' => '1', 'checked' => 'checked']];
$this->assertHtml($expected, $result);
}

/**
Expand Down

0 comments on commit 35af288

Please sign in to comment.