Skip to content

Commit

Permalink
Documenting function and addng another test case
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed Nov 20, 2013
1 parent 0e88f15 commit edd3796
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 2 deletions.
19 changes: 17 additions & 2 deletions Cake/ORM/Entity.php
Expand Up @@ -443,7 +443,22 @@ public function isNew($new = null) {
return $this->_persisted = (bool)$new;
}

public function validate(Validator $validator, array $fieldList) {
/**
* Validates the internal properties using a validator object. The resulting
* errors will be copied inside this entity and can be retrieved using the
* `errors` method.
*
* The second argument can be used to restrict the fields that need to be passed
* to the validator object.
*
* This function returns true if there were no validation errors or false
* otherwise.
*
* @param \Cake\Validation\Validator $validator
* @param array $fieldList The fields to be passed to the validator
* @return boolean
*/
public function validate(Validator $validator, array $fieldList = []) {
if (empty($fieldList)) {
$fieldList = array_keys($this->_properties);
}
Expand All @@ -460,7 +475,7 @@ public function validate(Validator $validator, array $fieldList) {
}

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

Expand Down
22 changes: 22 additions & 0 deletions Cake/Test/TestCase/ORM/EntityTest.php
Expand Up @@ -690,4 +690,26 @@ public function testValidateOnlyFieldsInList() {
$this->assertEquals(['something' => ['not valid']], $entity->errors());
}

/**
* Tests validate when called with no fieldList, it also tests the case when
* the validator returns no errors
*
* @return void
*/
public function testValidateNoFieldList() {
$validator = $this->getMock('\Cake\Validation\Validator');
$data = [
'a' => 'b',
'cool' => false,
'something' => true
];
$entity = new Entity($data);
$entity->isNew(true);

$validator->expects($this->once())->method('errors')
->with($data, true)
->will($this->returnValue([]));
$this->assertTrue($entity->validate($validator));
$this->assertEquals([], $entity->errors());
}
}

0 comments on commit edd3796

Please sign in to comment.