Skip to content

Commit

Permalink
Read schema defaults when generating field using EntityContext.
Browse files Browse the repository at this point in the history
  • Loading branch information
ADmad committed Apr 30, 2016
1 parent 6ba01e8 commit 4b9095c
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 4 deletions.
44 changes: 41 additions & 3 deletions src/View/Form/EntityContext.php
Expand Up @@ -201,16 +201,26 @@ public function isCreate()
* Traverses the entity data and finds the value for $path.
*
* @param string $field The dot separated path to the value.
* @param array $options Options:
* - `default`: Default value to return if no value found in request
* data or entity.
* - `schemaDefault`: Boolen indicating whether default value from table
* schema should be used if it's not explicitly provided.
* @return mixed The value of the field or null on a miss.
*/
public function val($field)
public function val($field, $options = [])
{
$options += [
'default' => null,
'schemaDefault' => true
];

$val = $this->_request->data($field);
if ($val !== null) {
return $val;
}
if (empty($this->_context['entity'])) {
return null;
return $options['default'];
}
$parts = explode('.', $field);
$entity = $this->entity($parts);
Expand All @@ -220,7 +230,15 @@ public function val($field)
}

if ($entity instanceof EntityInterface) {
return $entity->get(array_pop($parts));
$part = array_pop($parts);
$val = $entity->get($part);
if ($val !== null) {
return $val;
}
if (!$options['schemaDefault']) {
return $options['default'];
}
return $this->_schemaDefault($part, $entity);
}
if (is_array($entity)) {
$key = array_pop($parts);
Expand All @@ -229,6 +247,26 @@ public function val($field)
return null;
}

/**
* Get default value from table schema for given entity field.
*
* @param string $field Field name.
* @param \Cake\Datasource\EntityInterface $entity The entity.
* @return mixed
*/
protected function _schemaDefault($field, $entity)
{
$table = $this->_getTable($entity);
if ($table === false) {
return null;
}
$defaults = $table->schema()->defaultValues();
if (!array_key_exists($field, $defaults)) {
return null;
}
return $defaults[$field];
}

/**
* Helper method used to extract all the primary key values out of an array, The
* primary key column is guessed out of the provided $path array
Expand Down
6 changes: 5 additions & 1 deletion src/View/Helper/FormHelper.php
Expand Up @@ -2407,7 +2407,11 @@ protected function _initInputField($field, $options = [])
unset($options['value']);
}
if (!isset($options['val'])) {
$options['val'] = $context->val($field);
$valOptions = [
'default' => isset($options['default']) ? $options['default'] : null,
'schemaDefault' => isset($options['schemaDefault']) ? $options['schemaDefault'] : true,
];
$options['val'] = $context->val($field, $valOptions);
}
if (!isset($options['val']) && isset($options['default'])) {
$options['val'] = $options['default'];
Expand Down
20 changes: 20 additions & 0 deletions tests/TestCase/View/Form/EntityContextTest.php
Expand Up @@ -654,6 +654,26 @@ public function testValAssociatedCustomIds()
$this->assertEquals([1, 4], $result);
}

/**
* Test getting default value from table schema.
*
* @return void
*/
public function testValSchemaDefault()
{
$table = TableRegistry::get('Articles');
$column = $table->schema()->column('title');
$table->schema()->addColumn('title', ['default' => 'default title'] + $column);
$row = $table->newEntity();

$context = new EntityContext($this->request, [
'entity' => $row,
'table' => 'Articles',
]);
$result = $context->val('title');
$this->assertEquals('default title', $result);
}

/**
* Test validator for boolean fields.
*
Expand Down

0 comments on commit 4b9095c

Please sign in to comment.