Skip to content
Permalink
Browse files

allow to specify the source of injected form field values

  • Loading branch information...
Jonas Hartmann
Jonas Hartmann committed Jul 17, 2016
1 parent 117288d commit 521c42f7970785f49f95dd3a79e8fb0c02ffd5de
Showing with 29 additions and 2 deletions.
  1. +29 −2 src/View/Helper/FormHelper.php
@@ -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'];
}
@@ -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;
@@ -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'];
@@ -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.
You can’t perform that action at this time.