Browse Source

Starting to move data validation to the marshaller

Jose Lorenzo Rodriguez 11 years ago
parent
commit
a5784cd05e
2 changed files with 14 additions and 0 deletions
  1. 10 0
      src/ORM/Marshaller.php
  2. 4 0
      src/ORM/Table.php

+ 10 - 0
src/ORM/Marshaller.php

@@ -95,6 +95,7 @@ class Marshaller {
  * @see \Cake\ORM\Table::newEntity()
  */
 	public function one(array $data, array $options = []) {
+		$options += ['validate' => true];
 		$propertyMap = $this->_buildPropertyMap($options);
 
 		$schema = $this->_table->schema();
@@ -113,6 +114,13 @@ class Marshaller {
 			}
 		}
 
+		$errors = [];
+		if ($options['validate']) {
+			$type = is_string($options['validate']) ? $options['validate'] : 'default';
+			$validator = $this->_table->validator($type);
+			$errors = $validator->errors($data, $entity->isNew());
+		}
+
 		$primaryKey = $schema->primaryKey();
 		$properties = [];
 		foreach ($data as $key => $value) {
@@ -132,6 +140,7 @@ class Marshaller {
 
 		if (!isset($options['fieldList'])) {
 			$entity->set($properties);
+			$entity->errors($errors);
 			return $entity;
 		}
 
@@ -141,6 +150,7 @@ class Marshaller {
 			}
 		}
 
+		$entity->errors($errors);
 		return $entity;
 	}
 

+ 4 - 0
src/ORM/Table.php

@@ -1182,6 +1182,10 @@ class Table implements RepositoryInterface, EventListenerInterface {
 			'associated' => true
 		]);
 
+		if ($entity->errors()) {
+			return false;
+		}
+
 		if ($entity->isNew() === false && !$entity->dirty()) {
 			return $entity;
 		}