Skip to content

Commit

Permalink
Adding methods "validate" and "errors" to Entity
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed Nov 20, 2013
1 parent 476cb4c commit 0e88f15
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 0 deletions.
49 changes: 49 additions & 0 deletions Cake/ORM/Entity.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

use Cake\ORM\Table;
use Cake\Utility\Inflector;
use Cake\Validation\Validator;

/**
* An entity represents a single result row from a repository. It exposes the
Expand Down Expand Up @@ -62,6 +63,13 @@ class Entity implements \ArrayAccess, \JsonSerializable {
*/
protected $_persisted = null;

/**
* List of errors per field as stored in this object
*
* @var array
*/
protected $_errors = [];

/**
* Initializes the internal properties of this entity out of the
* keys in an array
Expand Down Expand Up @@ -412,6 +420,7 @@ public function dirty($property, $isDirty = null) {
*/
public function clean() {
$this->_dirty = [];
$this->_errors = [];
}

/**
Expand All @@ -434,4 +443,44 @@ public function isNew($new = null) {
return $this->_persisted = (bool)$new;
}

public function validate(Validator $validator, array $fieldList) {
if (empty($fieldList)) {
$fieldList = array_keys($this->_properties);
}

$missing = array_diff_key(array_flip($fieldList), $this->_properties);
$data = $this->extract($fieldList);

if ($missing) {
foreach ($data as $field => $value) {
if ($value === null && isset($missing[$field])) {
unset($data[$field]);
}
}
}

$new = $this->isNew();
$this->errors($validator->errors($data, $new === null ? true : false));
return empty($this->_errors);
}

public function errors($field = null, $errors = null) {
if ($field === null) {
return $this->_errors;
}

if (is_string($field) && $errors === null && isset($this->_errors[$field])) {
return $this->_errors[$field];
}

if (!is_array($field)) {
$field = [$field => $errors];
}

foreach ($field as $f => $error) {
$this->_errors[$f] = (array)$error;
}

return $this;
}
}
42 changes: 42 additions & 0 deletions Cake/Test/TestCase/ORM/EntityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

use Cake\ORM\Entity;
use Cake\TestSuite\TestCase;
use Cake\Validation\Validator;

/**
* Entity test case.
Expand Down Expand Up @@ -648,4 +649,45 @@ public function testToArrayWithAccessor() {
$this->assertEquals($expected, $entity->toArray());
}

/**
* Tests that missing fields will not be passed as null to the validator
*
* @return void
*/
public function testValidateMissingFields() {
$entity = $this->getMockBuilder('\Cake\ORM\Entity')
->setMethods(['getSomething'])
->disableOriginalConstructor()
->getMock();
$validator = $this->getMock('\Cake\Validation\Validator');
$entity->set('a', 'b');

$validator->expects($this->once())->method('errors')
->with(['a' => 'b'], true)
->will($this->returnValue(['a' => ['not valid']]));
$this->assertFalse($entity->validate($validator, ['a', 'something']));
$this->assertEquals(['a' => ['not valid']], $entity->errors());
}

/**
* Tests that only fields in the passed list are validated
*
* @return void
*/
public function testValidateOnlyFieldsInList() {
$validator = $this->getMock('\Cake\Validation\Validator');
$entity = new Entity([
'a' => 'b',
'cool' => false,
'something' => true
]);
$entity->isNew(false);

$validator->expects($this->once())->method('errors')
->with(['a' => 'b', 'something' => true], false)
->will($this->returnValue(['something' => ['not valid']]));
$this->assertFalse($entity->validate($validator, ['a', 'something']));
$this->assertEquals(['something' => ['not valid']], $entity->errors());
}

}

0 comments on commit 0e88f15

Please sign in to comment.