Skip to content

Commit

Permalink
allow to specify the source of injected form field values
Browse files Browse the repository at this point in the history
  • Loading branch information
Jonas Hartmann committed Jul 17, 2016
1 parent 117288d commit 521c42f
Showing 1 changed file with 29 additions and 2 deletions.
31 changes: 29 additions & 2 deletions src/View/Helper/FormHelper.php
Expand Up @@ -359,12 +359,15 @@ public function create($model = null, array $options = [])
'encoding' => strtolower(Configure::read('App.encoding')),
'templates' => null,
'idPrefix' => null,
'values' => ['data', 'context'],
];

if (isset($options['action'])) {
trigger_error('Using key `action` is deprecated, use `url` directly instead.', E_USER_DEPRECATED);
}

$this->setValuesSources($options['values']);

if ($options['idPrefix'] !== null) {
$this->_idPrefix = $options['idPrefix'];
}
Expand Down Expand Up @@ -472,7 +475,7 @@ protected function _formUrl($context, $options)

$pk = $context->primaryKey();
if (count($pk)) {
$id = $context->val($pk[0]);
$id = $this->getSourceValue($pk[0]);
}
if (empty($action[0]) && isset($id)) {
$action[0] = $id;
Expand Down Expand Up @@ -2466,7 +2469,7 @@ protected function _initInputField($field, $options = [])
unset($options['value']);
}
if (!isset($options['val'])) {
$options['val'] = $context->val($field);
$options['val'] = $this->getSourceValue($field);
}
if (!isset($options['val']) && isset($options['default'])) {
$options['val'] = $options['default'];
Expand Down Expand Up @@ -2672,4 +2675,28 @@ public function implementedEvents()
{
return [];
}

public function getValuesSources()
{
return $this->_valuesSources;
}

public function setValuesSources($sources)
{
$this->_valuesSources = array_values(array_intersect((array)$sources, ['context', 'data', 'query']));
return $this;
}

public function getSourceValue($fieldname)
{
foreach ($this->getValuesSources() as $valuesSource) {
if ($valuesSource === 'context') {
return $this->_getContext()->val($fieldname);
}
if (isset($this->request->{$valuesSource}[$fieldname])) {
return $this->request->{$valuesSource}[$fieldname];
}
}
}

}

0 comments on commit 521c42f

Please sign in to comment.