diff --git a/src/View/Form/EntityContext.php b/src/View/Form/EntityContext.php index 2ba0a71a46f..e575cee26c2 100644 --- a/src/View/Form/EntityContext.php +++ b/src/View/Form/EntityContext.php @@ -323,6 +323,9 @@ public function hasError($field) { public function error($field) { $parts = explode('.', $field); list($entity, $prop) = $this->_getEntity($parts); + if (!$entity) { + return []; + } return $entity->errors(array_pop($parts)); } diff --git a/tests/TestCase/View/Form/EntityContextTest.php b/tests/TestCase/View/Form/EntityContextTest.php index 65d2760e2f7..d5470852183 100644 --- a/tests/TestCase/View/Form/EntityContextTest.php +++ b/tests/TestCase/View/Form/EntityContextTest.php @@ -66,6 +66,26 @@ public function testInvalidTable() { ]); } +/** + * Test operations with no entity. + * + * @return void + */ + public function testOperationsNoEntity() { + $context = new EntityContext($this->request, [ + 'table' => 'Articles' + ]); + + $this->assertNull($context->val('title')); + $this->assertFalse($context->isRequired('title')); + $this->assertFalse($context->hasError('title')); + $this->assertEquals('string', $context->type('title')); + $this->assertEquals([], $context->error('title')); + $this->assertEquals( + ['length' => null, 'precision' => null], + $context->attributes('title')); + } + /** * Test operations that lack a table argument. *