Permalink
Browse files

Add hasError and error to EntityContext.

  • Loading branch information...
1 parent 709e023 commit d7d94e774bde40b4bd2d92fad01186ced3daa8e6 @markstory markstory committed Feb 3, 2014
Showing with 110 additions and 0 deletions.
  1. +27 −0 src/View/Form/EntityContext.php
  2. +83 −0 tests/TestCase/View/Form/EntityContextTest.php
@@ -285,10 +285,37 @@ public function attributes($field) {
return array_intersect_key($column, $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) {
+ $parts = explode('.', $field);
+ list($entity, $prop) = $this->_getEntity($parts);
+ if (!$entity) {
+ return false;
+ }
+ $errors = $entity->errors(array_pop($parts));
+ 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.
+ */
public function error($field) {
+ $parts = explode('.', $field);
+ list($entity, $prop) = $this->_getEntity($parts);
+ if (!$entity) {
+ return false;
+ }
+ return $entity->errors(array_pop($parts));
}
}
@@ -314,6 +314,89 @@ public function testAttributes() {
$this->assertEquals($expected, $context->attributes('user.rating'));
}
+/**
+ * Test hasError
+ *
+ * @return void
+ */
+ public function testHasError() {
+ $this->_setupTables();
+
+ $row = new Entity([
+ 'title' => 'My title',
+ 'user' => new Entity(['username' => 'Mark']),
+ ]);
+ $row->errors('title', []);
+ $row->errors('body', 'Gotta have one');
+ $row->errors('user_id', ['Required field']);
+ $context = new EntityContext($this->request, [
+ 'entity' => $row,
+ 'table' => 'Articles',
+ ]);
+
+ $this->assertFalse($context->hasError('title'));
+ $this->assertFalse($context->hasError('nope'));
+ $this->assertTrue($context->hasError('body'));
+ $this->assertTrue($context->hasError('user_id'));
+ }
+
+/**
+ * Test hasError on associated records
+ *
+ * @return void
+ */
+ public function testHasErrorAssociated() {
+ $this->_setupTables();
+
+ $row = new Entity([
+ 'title' => 'My title',
+ 'user' => new Entity(['username' => 'Mark']),
+ ]);
+ $row->errors('title', []);
+ $row->errors('body', 'Gotta have one');
+ $row->user->errors('username', ['Required']);
+ $context = new EntityContext($this->request, [
+ 'entity' => $row,
+ 'table' => 'Articles',
+ ]);
+
+ $this->assertTrue($context->hasError('user.username'));
+ $this->assertFalse($context->hasError('user.nope'));
+ $this->assertFalse($context->hasError('no.nope'));
+ }
+
+/**
+ * Test error
+ *
+ * @return void
+ */
+ public function testError() {
+ $this->_setupTables();
+
+ $row = new Entity([
+ 'title' => 'My title',
+ 'user' => new Entity(['username' => 'Mark']),
+ ]);
+ $row->errors('title', []);
+ $row->errors('body', 'Gotta have one');
+ $row->errors('user_id', ['Required field']);
+
+ $row->user->errors('username', ['Required']);
+
+ $context = new EntityContext($this->request, [
+ 'entity' => $row,
+ 'table' => 'Articles',
+ ]);
+
+ $this->assertEquals([], $context->error('title'));
+
+ $expected = ['Gotta have one'];
+ $this->assertEquals($expected, $context->error('body'));
+
+ $expected = ['Required'];
+ $this->assertEquals($expected, $context->error('user.username'));
+ }
+
/**
* Setup tables for tests.
*

0 comments on commit d7d94e7

Please sign in to comment.