Skip to content

Commit dce5292

Browse files
committed
Adding validation to the saving process
1 parent a281906 commit dce5292

File tree

2 files changed

+98
-1
lines changed

2 files changed

+98
-1
lines changed

Cake/ORM/Table.php

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -887,7 +887,11 @@ public function exists(array $conditions) {
887887
* @return \Cake\ORM\Entity|boolean
888888
*/
889889
public function save(Entity $entity, array $options = []) {
890-
$options = new \ArrayObject($options + ['atomic' => true, 'fieldList' => []]);
890+
$options = new \ArrayObject($options + [
891+
'atomic' => true,
892+
'fieldList' => [],
893+
'validate' => true
894+
]);
891895
if ($options['atomic']) {
892896
$connection = $this->connection();
893897
$success = $connection->transactional(function() use ($entity, $options) {
@@ -917,6 +921,10 @@ protected function _processSave($entity, $options) {
917921
$entity->isNew(true);
918922
}
919923

924+
if (!$this->_processValidation($entity, $options)) {
925+
return false;
926+
}
927+
920928
$event = new Event('Model.beforeSave', $this, compact('entity', 'options'));
921929
$this->getEventManager()->dispatch($event);
922930

@@ -1034,6 +1042,28 @@ protected function _update($entity, $data) {
10341042
return $success;
10351043
}
10361044

1045+
/**
1046+
* Validates the $entity if the 'validate' key is not set to false in $options
1047+
*
1048+
* @param \Cake\ORM\Entity The entity to validate
1049+
* @param \ArrayObject $options
1050+
* @return boolean true if the entity is valid, false otherwise
1051+
*/
1052+
protected function _processValidation($entity, $options) {
1053+
if (empty($options['validate'])) {
1054+
return true;
1055+
}
1056+
1057+
$type = is_string($options['validate']) ? $options['validate'] : 'default';
1058+
$validator = $this->validator($type);
1059+
1060+
if (!count($validator)) {
1061+
return true;
1062+
}
1063+
1064+
return $entity->validate($validator, $options['fieldList']);
1065+
}
1066+
10371067
/**
10381068
* Delete a single entity.
10391069
*

Cake/Test/TestCase/ORM/TableTest.php

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
use Cake\Database\Expression\QueryExpression;
2222
use Cake\ORM\Table;
2323
use Cake\ORM\TableRegistry;
24+
use Cake\Validation\Validator;
2425

2526
/**
2627
* Used to test correct class is instantiated when using TableRegistry::get();
@@ -1836,4 +1837,70 @@ public function testValidatorSetter() {
18361837
$this->assertSame($validator, $table->validator('other'));
18371838
}
18381839

1840+
/**
1841+
* Tests saving with validation
1842+
*
1843+
* @return void
1844+
*/
1845+
public function testSaveWithValidationError() {
1846+
$entity = new \Cake\ORM\Entity([
1847+
'username' => 'superuser'
1848+
]);
1849+
$table = TableRegistry::get('users');
1850+
$table->validator()->validatePresence('password');
1851+
$this->assertFalse($table->save($entity));
1852+
$this->assertNotEmpty($entity->errors('password'));
1853+
}
1854+
1855+
/**
1856+
* Tests saving with validation and field list
1857+
*
1858+
* @return void
1859+
*/
1860+
public function testSaveWithValidationErrorAndFieldList() {
1861+
$entity = new \Cake\ORM\Entity([
1862+
'username' => 'superuser'
1863+
]);
1864+
$table = TableRegistry::get('users');
1865+
$table->validator()->validatePresence('password');
1866+
$this->assertFalse($table->save($entity, [
1867+
'fieldList' => ['username', 'password']
1868+
]));
1869+
$this->assertNotEmpty($entity->errors('password'));
1870+
}
1871+
1872+
/**
1873+
* Tests using a custom validation object when saving
1874+
*
1875+
* @return void
1876+
*/
1877+
public function testSaveWithDifferentValidator() {
1878+
$entity = new \Cake\ORM\Entity([
1879+
'username' => 'superuser'
1880+
]);
1881+
$table = TableRegistry::get('users');
1882+
$validator = (new Validator)->validatePresence('password');
1883+
$table->validator('custom', $validator);
1884+
$this->assertFalse($table->save($entity, ['validate' => 'custom']));
1885+
$this->assertNotEmpty($entity->errors('password'));
1886+
1887+
$this->assertSame($entity, $table->save($entity), 'default was not used');
1888+
}
1889+
1890+
/**
1891+
* Tests saving with successful validation
1892+
*
1893+
* @return void
1894+
*/
1895+
public function testSaveWithValidationSuccess() {
1896+
$entity = new \Cake\ORM\Entity([
1897+
'username' => 'superuser',
1898+
'password' => 'hey'
1899+
]);
1900+
$table = TableRegistry::get('users');
1901+
$table->validator()->validatePresence('password');
1902+
$this->assertSame($entity, $table->save($entity));
1903+
$this->assertEmpty($entity->errors('password'));
1904+
}
1905+
18391906
}

0 commit comments

Comments
 (0)