diff --git a/src/View/Form/EntityContext.php b/src/View/Form/EntityContext.php index de9bb824327..0c999704535 100644 --- a/src/View/Form/EntityContext.php +++ b/src/View/Form/EntityContext.php @@ -267,7 +267,22 @@ public function type($field) { return $table->schema()->columnType($column); } +/** + * Get an associative array of other attributes for a field name. + * + * @param string $field A dot separated path to get additional data on. + * @return array An array of data describing the additional attributes on a field. + */ public function attributes($field) { + $parts = explode('.', $field); + list($entity, $prop) = $this->_getEntity($parts); + if (!$entity) { + return null; + } + $table = $this->_getTable($prop); + $column = $table->schema()->column(array_pop($parts)); + $whitelist = ['length' => null, 'precision' => null]; + return array_intersect_key($column, $whitelist); } public function hasError($field) { diff --git a/tests/TestCase/View/Form/EntityContextTest.php b/tests/TestCase/View/Form/EntityContextTest.php index a2c65d6c2d1..49375a175e1 100644 --- a/tests/TestCase/View/Form/EntityContextTest.php +++ b/tests/TestCase/View/Form/EntityContextTest.php @@ -297,6 +297,21 @@ public function testAttributes() { 'entity' => $row, 'table' => 'Articles', ]); + + $expected = [ + 'length' => 255, 'precision' => null + ]; + $this->assertEquals($expected, $context->attributes('title')); + + $expected = [ + 'length' => null, 'precision' => null + ]; + $this->assertEquals($expected, $context->attributes('body')); + + $expected = [ + 'length' => 10, 'precision' => 3 + ]; + $this->assertEquals($expected, $context->attributes('user.rating')); } /** @@ -321,7 +336,8 @@ protected function _setupTables() { $users->schema([ 'id' => ['type' => 'integer', 'length' => 11], 'username' => ['type' => 'string', 'length' => 255], - 'bio' => ['type' => 'text'] + 'bio' => ['type' => 'text'], + 'rating' => ['type' => 'decimal', 'length' => 10, 'precision' => 3], ]); $validator = new Validator();