Browse Source

Add error() and hasError()

These are important features for generating validation messages.
mark_story 12 years ago
parent
commit
fd950cc628
2 changed files with 75 additions and 2 deletions
  1. 33 2
      src/View/Form/ArrayContext.php
  2. 42 0
      tests/TestCase/View/Form/ArrayContextTest.php

+ 33 - 2
src/View/Form/ArrayContext.php

@@ -25,13 +25,16 @@ use Cake\Utility\Hash;
  * Important keys:
  *
  * - `defaults` The default values for fields. These values
- *   will be used when there is no request data set.
+ *   will be used when there is no request data set. Data should be nested following
+ *   the dot separated paths you access your fields with.
  * - `required` A nested array of fields, relationships and boolean
  *   flags to indicate a field is required.
- * - `schema` An array of data that emulate the structures that
+ * - `schema` An array of data that emulate the column structures that
  *   Cake\Database\Schema\Table uses. This array allows you to control
  *   the inferred type for fields and allows auto generation of attributes
  *   like maxlength, step and other HTML attributes.
+ * - `errors` An array of validation errors. Errors should be nested following
+ *   the dot separated paths you access your fields with.
  */
 class ArrayContext {
 
@@ -61,6 +64,7 @@ class ArrayContext {
 			'schema' => [],
 			'required' => [],
 			'defaults' => [],
+			'errors' => [],
 		];
 		$this->_context = $context;
 	}
@@ -132,4 +136,31 @@ class ArrayContext {
 		return array_intersect_key($schema, $whitelist);
 	}
 
+/**
+ * Check whether or not a field has an error attached to it
+ *
+ * @param string $field A dot separated path to check errors on.
+ * @return boolean Returns true if the errors for the field are not empty.
+ */
+	public function hasError($field) {
+		if (empty($this->_context['errors'])) {
+			return false;
+		}
+		return (bool)Hash::check($this->_context['errors'], $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.
+ */
+	public function error($field) {
+		if (empty($this->_context['errors'])) {
+			return null;
+		}
+		return Hash::get($this->_context['errors'], $field);
+	}
+
 }

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

@@ -147,4 +147,46 @@ class ArrayContextTest extends TestCase {
 		$this->assertEquals(['precision' => 2, 'length' => 5], $context->attributes('Comments.floaty'));
 	}
 
+/**
+ * Test fetching errors.
+ *
+ * @return void
+ */
+	public function testError() {
+		$context = new ArrayContext($this->request, [
+			'errors' => [
+				'Comments' => [
+					'comment' => ['Comment is required'],
+					'empty' => [],
+					'user_id' => 'A valid userid is required',
+				]
+			]
+		]);
+		$this->assertEquals(['Comment is required'], $context->error('Comments.comment'));
+		$this->assertEquals('A valid userid is required', $context->error('Comments.user_id'));
+		$this->assertEquals([], $context->error('Comments.empty'));
+		$this->assertNull($context->error('Comments.not_there'));
+	}
+
+/**
+ * Test checking errors.
+ *
+ * @return void
+ */
+	public function testHasError() {
+		$context = new ArrayContext($this->request, [
+			'errors' => [
+				'Comments' => [
+					'comment' => ['Comment is required'],
+					'empty' => [],
+					'user_id' => 'A valid userid is required',
+				]
+			]
+		]);
+		$this->assertFalse($context->hasError('Comments.not_there'));
+		$this->assertFalse($context->hasError('Comments.empty'));
+		$this->assertTrue($context->hasError('Comments.user_id'));
+		$this->assertTrue($context->hasError('Comments.comment'));
+	}
+
 }