Skip to content

Commit

Permalink
Fix return type of error()
Browse files Browse the repository at this point in the history
Make it consistent (always an array). Also make the two implementations
agree on a return type.
  • Loading branch information
markstory committed Feb 3, 2014
1 parent d7d94e7 commit 86c0fa0
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 7 deletions.
6 changes: 3 additions & 3 deletions src/View/Form/ArrayContext.php
Expand Up @@ -154,12 +154,12 @@ public function hasError($field) {
* Get the errors for a given field
*
* @param string $field A dot separated path to check errors on.
* @return mixed Either a string or an array of errors. Null
* will be returned when the field path is undefined.
* @return array An array of errors, an empty array will be returned when the
* context has no errors.
*/
public function error($field) {
if (empty($this->_context['errors'])) {
return null;
return [];
}
return Hash::get($this->_context['errors'], $field);
}
Expand Down
6 changes: 2 additions & 4 deletions src/View/Form/EntityContext.php
Expand Up @@ -301,19 +301,17 @@ public function hasError($field) {
return !empty($errors);
}


/**
* Get the errors for a given field
*
* @param string $field A dot separated path to check errors on.
* @return array|null Either an array of errors. Null will be returned when the
* field path is undefined or there is no error.
* @return array An array of errors.
*/
public function error($field) {
$parts = explode('.', $field);
list($entity, $prop) = $this->_getEntity($parts);
if (!$entity) {
return false;
return [];
}
return $entity->errors(array_pop($parts));
}
Expand Down
3 changes: 3 additions & 0 deletions tests/TestCase/View/Form/ArrayContextTest.php
Expand Up @@ -153,6 +153,9 @@ public function testAttributes() {
* @return void
*/
public function testError() {
$context = new ArrayContext($this->request, []);
$this->assertEquals([], $context->error('Comments.empty'));

$context = new ArrayContext($this->request, [
'errors' => [
'Comments' => [
Expand Down

0 comments on commit 86c0fa0

Please sign in to comment.