Skip to content
Permalink
Browse files

Fix issue when providing explicit default value for input.

  • Loading branch information...
ADmad committed Apr 29, 2016
1 parent 4b9095c commit 35af288656cc3842c8619217e75ae8b989e04d34
Showing with 77 additions and 1 deletion.
  1. +4 −1 src/View/Form/EntityContext.php
  2. +73 −0 tests/TestCase/View/Helper/FormHelperTest.php
@@ -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);
@@ -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);
}
/**
@@ -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
*
@@ -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);
}
/**

0 comments on commit 35af288

Please sign in to comment.
You can’t perform that action at this time.