Skip to content
Permalink
Browse files

Read schema defaults when generating field using EntityContext.

  • Loading branch information...
ADmad committed Apr 29, 2016
1 parent 6ba01e8 commit 4b9095c10ca3caa5e0c9e66283b1b7411076fe06
Showing with 66 additions and 4 deletions.
  1. +41 −3 src/View/Form/EntityContext.php
  2. +5 −1 src/View/Helper/FormHelper.php
  3. +20 −0 tests/TestCase/View/Form/EntityContextTest.php
@@ -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);
@@ -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);
@@ -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
@@ -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'];
@@ -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.
*

0 comments on commit 4b9095c

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