Skip to content

Commit

Permalink
Add error() and hasError()
Browse files Browse the repository at this point in the history
These are important features for generating validation messages.
  • Loading branch information
markstory committed Jan 30, 2014
1 parent 669cd69 commit fd950cc
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 2 deletions.
35 changes: 33 additions & 2 deletions src/View/Form/ArrayContext.php
Expand Up @@ -25,13 +25,16 @@
* 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 {

Expand Down Expand Up @@ -61,6 +64,7 @@ public function __construct(Request $request, array $context) {
'schema' => [],
'required' => [],
'defaults' => [],
'errors' => [],
];
$this->_context = $context;
}
Expand Down Expand Up @@ -132,4 +136,31 @@ public function attributes($field) {
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 changes: 42 additions & 0 deletions tests/TestCase/View/Form/ArrayContextTest.php
Expand Up @@ -147,4 +147,46 @@ public function testAttributes() {
$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'));
}

}

0 comments on commit fd950cc

Please sign in to comment.