Skip to content

Commit 86c0fa0

Browse files
committed
Fix return type of error()
Make it consistent (always an array). Also make the two implementations agree on a return type.
1 parent d7d94e7 commit 86c0fa0

File tree

3 files changed

+8
-7
lines changed

3 files changed

+8
-7
lines changed

src/View/Form/ArrayContext.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -154,12 +154,12 @@ public function hasError($field) {
154154
* Get the errors for a given field
155155
*
156156
* @param string $field A dot separated path to check errors on.
157-
* @return mixed Either a string or an array of errors. Null
158-
* will be returned when the field path is undefined.
157+
* @return array An array of errors, an empty array will be returned when the
158+
* context has no errors.
159159
*/
160160
public function error($field) {
161161
if (empty($this->_context['errors'])) {
162-
return null;
162+
return [];
163163
}
164164
return Hash::get($this->_context['errors'], $field);
165165
}

src/View/Form/EntityContext.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -301,19 +301,17 @@ public function hasError($field) {
301301
return !empty($errors);
302302
}
303303

304-
305304
/**
306305
* Get the errors for a given field
307306
*
308307
* @param string $field A dot separated path to check errors on.
309-
* @return array|null Either an array of errors. Null will be returned when the
310-
* field path is undefined or there is no error.
308+
* @return array An array of errors.
311309
*/
312310
public function error($field) {
313311
$parts = explode('.', $field);
314312
list($entity, $prop) = $this->_getEntity($parts);
315313
if (!$entity) {
316-
return false;
314+
return [];
317315
}
318316
return $entity->errors(array_pop($parts));
319317
}

tests/TestCase/View/Form/ArrayContextTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,9 @@ public function testAttributes() {
153153
* @return void
154154
*/
155155
public function testError() {
156+
$context = new ArrayContext($this->request, []);
157+
$this->assertEquals([], $context->error('Comments.empty'));
158+
156159
$context = new ArrayContext($this->request, [
157160
'errors' => [
158161
'Comments' => [

0 commit comments

Comments
 (0)