diff --git a/src/View/Form/EntityContext.php b/src/View/Form/EntityContext.php index 595722c439c..06427349769 100644 --- a/src/View/Form/EntityContext.php +++ b/src/View/Form/EntityContext.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 diff --git a/src/View/Helper/FormHelper.php b/src/View/Helper/FormHelper.php index 8256814574d..d776b740095 100644 --- a/src/View/Helper/FormHelper.php +++ b/src/View/Helper/FormHelper.php @@ -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']; diff --git a/tests/TestCase/View/Form/EntityContextTest.php b/tests/TestCase/View/Form/EntityContextTest.php index bd8cb641ff5..84ec59f9f92 100644 --- a/tests/TestCase/View/Form/EntityContextTest.php +++ b/tests/TestCase/View/Form/EntityContextTest.php @@ -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. *