Skip to content

Commit

Permalink
Not using fieldList for validation anymore
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed Nov 20, 2013
1 parent 5c2acfc commit 27728be
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 46 deletions.
22 changes: 2 additions & 20 deletions Cake/ORM/Entity.php
Expand Up @@ -449,32 +449,14 @@ public function isNew($new = null) {
* 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);
}

$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]);
}
}
}

public function validate(Validator $validator) {
$data = $this->toArray();
$new = $this->isNew();
$this->errors($validator->errors($data, $new === null ? true : $new));
return empty($this->_errors);
Expand Down
2 changes: 1 addition & 1 deletion Cake/ORM/Table.php
Expand Up @@ -1090,7 +1090,7 @@ protected function _processValidation($entity, $options) {
return true;
}

$success = $entity->validate($validator, $options['fieldList']);
$success = $entity->validate($validator);

$event = new Event('Model.afterValidate', $this, $pass);
$this->getEventManager()->dispatch($event);
Expand Down
28 changes: 3 additions & 25 deletions Cake/Test/TestCase/ORM/EntityTest.php
Expand Up @@ -665,38 +665,16 @@ public function testValidateMissingFields() {
$validator->expects($this->once())->method('errors')
->with(['a' => 'b'], true)
->will($this->returnValue(['a' => ['not valid']]));
$this->assertFalse($entity->validate($validator, ['a', 'something']));
$this->assertFalse($entity->validate($validator));
$this->assertEquals(['a' => ['not valid']], $entity->errors());
}

/**
* Tests that only fields in the passed list are validated
* Tests validate when the validator returns no errors
*
* @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());
}

/**
* Tests validate when called with no fieldList, it also tests the case when
* the validator returns no errors
*
* @return void
*/
public function testValidateNoFieldList() {
public function testValidateSuccess() {
$validator = $this->getMock('\Cake\Validation\Validator');
$data = [
'a' => 'b',
Expand Down

0 comments on commit 27728be

Please sign in to comment.