diff --git a/src/View/Form/ArrayContext.php b/src/View/Form/ArrayContext.php index 98b48c316b5..f9cc3f7eb54 100644 --- a/src/View/Form/ArrayContext.php +++ b/src/View/Form/ArrayContext.php @@ -206,6 +206,14 @@ public function isRequired($field) return (bool)$required; } + /** + * {@inheritDoc} + */ + public function getErrorMessage($field) + { + return null; + } + /** * {@inheritDoc} */ diff --git a/src/View/Form/ContextInterface.php b/src/View/Form/ContextInterface.php index 3daa8f59c8f..47efdd19c8a 100644 --- a/src/View/Form/ContextInterface.php +++ b/src/View/Form/ContextInterface.php @@ -70,6 +70,14 @@ public function val($field); */ public function isRequired($field); + /** + * Gets the default "required" error message for a field + * + * @param string $field A dot separated path to the field + * @return string + */ + public function getErrorMessage($field); + /** * Get the fieldnames of the top level object in this context. * diff --git a/src/View/Form/EntityContext.php b/src/View/Form/EntityContext.php index 871d9b46f49..d0ec4ee8b4f 100644 --- a/src/View/Form/EntityContext.php +++ b/src/View/Form/EntityContext.php @@ -426,6 +426,34 @@ public function isRequired($field) return false; } + /** + * {@inheritDoc} + */ + public function getErrorMessage($field) + { + $parts = explode('.', $field); + + $validator = $this->_getValidator($parts); + $fieldName = array_pop($parts); + if (!$validator->hasField($fieldName)) { + return null; + } + + $ruleset = $validator->field($fieldName); + + $presenceErrors = $validator->errors([]); + $emptyErrors = $validator->errors([$fieldName => '']); + + if ($ruleset->isPresenceRequired() && isset($presenceErrors[$fieldName])) { + return $presenceErrors[$fieldName]['_required'] ?? null; + } + if (!$ruleset->isEmptyAllowed() && isset($emptyErrors[$fieldName])) { + return $emptyErrors[$fieldName]['_empty'] ?? null; + } + + return null; + } + /** * Get the field names from the top level entity. * diff --git a/src/View/Form/FormContext.php b/src/View/Form/FormContext.php index e1616014eaa..cc657baff7b 100644 --- a/src/View/Form/FormContext.php +++ b/src/View/Form/FormContext.php @@ -134,6 +134,14 @@ public function isRequired($field) return false; } + /** + * {@inheritDoc} + */ + public function getErrorMessage($field) + { + return null; + } + /** * {@inheritDoc} */ diff --git a/src/View/Form/NullContext.php b/src/View/Form/NullContext.php index f2e38b11e77..af3e889fba5 100644 --- a/src/View/Form/NullContext.php +++ b/src/View/Form/NullContext.php @@ -83,6 +83,14 @@ public function isRequired($field) return false; } + /** + * {@inheritDoc} + */ + public function getErrorMessage($field) + { + return null; + } + /** * {@inheritDoc} */ diff --git a/tests/TestCase/View/Form/EntityContextTest.php b/tests/TestCase/View/Form/EntityContextTest.php index 088a26170bb..f543f0e8051 100644 --- a/tests/TestCase/View/Form/EntityContextTest.php +++ b/tests/TestCase/View/Form/EntityContextTest.php @@ -66,6 +66,25 @@ public function setUp() $this->request = new ServerRequest(); } + /** + * tests getMessage + * + * @return void + */ + public function testGetMessage() + { + $this->_setupTables(); + + $context = new EntityContext($this->request, [ + 'entity' => new Article(), + 'table' => 'Articles', + 'validator' => 'create', + ]); + + $this->assertNull($context->getErrorMessage('body')); + $this->assertSame('Don\'t forget a title!', $context->getErrorMessage('title')); + } + /** * Test getting entity back from context. * @@ -1283,6 +1302,7 @@ protected function _setupTables() ]); $validator = new Validator(); + $validator->requirePresence('title', true, 'Don\'t forget a title!'); $validator->add('title', 'minlength', [ 'rule' => ['minlength', 10] ])