Browse Source

Fix return type of error()

Make it consistent (always an array). Also make the two implementations
agree on a return type.
mark_story 12 years ago
parent
commit
86c0fa0212

+ 3 - 3
src/View/Form/ArrayContext.php

@@ -154,12 +154,12 @@ class ArrayContext {
  * 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);
 	}

+ 2 - 4
src/View/Form/EntityContext.php

@@ -301,19 +301,17 @@ class EntityContext {
 		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));
 	}

+ 3 - 0
tests/TestCase/View/Form/ArrayContextTest.php

@@ -153,6 +153,9 @@ class ArrayContextTest extends TestCase {
  * @return void
  */
 	public function testError() {
+		$context = new ArrayContext($this->request, []);
+		$this->assertEquals([], $context->error('Comments.empty'));
+
 		$context = new ArrayContext($this->request, [
 			'errors' => [
 				'Comments' => [