Skip to content

Commit

Permalink
made source context pass through to next source if no value found for…
Browse files Browse the repository at this point in the history
… context source
  • Loading branch information
Jonas Hartmann committed Aug 26, 2016
1 parent 4eecbfe commit f7d1ddd
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 9 deletions.
15 changes: 6 additions & 9 deletions src/View/Helper/FormHelper.php
Expand Up @@ -2733,17 +2733,14 @@ public function getSourceValue($fieldname, $options = [])
{
foreach ($this->getValuesSources() as $valuesSource) {
if ($valuesSource === 'context') {
return $this->_getContext()->val($fieldname, $options);
}
if ($this->request->{$valuesSource}($fieldname) !== null) {
$val = $this->_getContext()->val($fieldname, $options);
if ($val !== null) {
return $val;
}
} elseif ($this->request->{$valuesSource}($fieldname) !== null) {
return $this->request->{$valuesSource}($fieldname);
}
}
if (isset($options['default'])) {
return $options['default'];
}
if (isset($options['schemaDefault'])) {
return $options['schemaDefault'];
}
return null;
}
}
25 changes: 25 additions & 0 deletions tests/TestCase/View/Helper/FormHelperTest.php
Expand Up @@ -8556,4 +8556,29 @@ public function testFormValuesSourcesDefaults()
$expected = ['input' => ['type' => 'password', 'name' => 'password', 'value' => 'helloworld']];
$this->assertHtml($expected, $result);
}


/**
* Test sources values schema defaults handling
*
* @return void
*/
public function testSourcesValueDoesntExistPassThrough()
{
$this->Form->request->query['category'] = 'sesame-cookies';

$Articles = TableRegistry::get('Articles');
$entity = $Articles->newEntity();
$this->Form->create($entity);

$this->Form->setValuesSources(['query', 'context']);
$result = $this->Form->getSourceValue('category');
$expected = 'sesame-cookies';
$this->assertEquals($expected, $result);

$this->Form->setValuesSources(['context', 'query']);
$result = $this->Form->getSourceValue('category');
$expected = 'sesame-cookies';
$this->assertEquals($expected, $result);
}
}

0 comments on commit f7d1ddd

Please sign in to comment.