Skip to content

Commit

Permalink
Added ContextInterface::getErrorMessage and EntityContext implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremyharris committed Mar 17, 2018
1 parent a9a0199 commit 5823d82
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/View/Form/ArrayContext.php
Expand Up @@ -206,6 +206,14 @@ public function isRequired($field)
return (bool)$required;
}

/**
* {@inheritDoc}
*/
public function getErrorMessage($field)
{
return null;
}

/**
* {@inheritDoc}
*/
Expand Down
8 changes: 8 additions & 0 deletions src/View/Form/ContextInterface.php
Expand Up @@ -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.
*
Expand Down
28 changes: 28 additions & 0 deletions src/View/Form/EntityContext.php
Expand Up @@ -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.
*
Expand Down
8 changes: 8 additions & 0 deletions src/View/Form/FormContext.php
Expand Up @@ -134,6 +134,14 @@ public function isRequired($field)
return false;
}

/**
* {@inheritDoc}
*/
public function getErrorMessage($field)
{
return null;
}

/**
* {@inheritDoc}
*/
Expand Down
8 changes: 8 additions & 0 deletions src/View/Form/NullContext.php
Expand Up @@ -83,6 +83,14 @@ public function isRequired($field)
return false;
}

/**
* {@inheritDoc}
*/
public function getErrorMessage($field)
{
return null;
}

/**
* {@inheritDoc}
*/
Expand Down
20 changes: 20 additions & 0 deletions tests/TestCase/View/Form/EntityContextTest.php
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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]
])
Expand Down

0 comments on commit 5823d82

Please sign in to comment.