Merge pull request #1000 from dogmatic69/validation-simplify

Validation simplify

Simplify internals in Model/Validation
This commit is contained in:
Mark Story 2012-12-07 19:00:46 -08:00
commit 38c592928d

View file

@ -130,11 +130,10 @@ class ModelValidator implements ArrayAccess, IteratorAggregate, Countable {
$options = array_merge(array('atomic' => true, 'deep' => false), $options); $options = array_merge(array('atomic' => true, 'deep' => false), $options);
$model->validationErrors = $validationErrors = $return = array(); $model->validationErrors = $validationErrors = $return = array();
$model->create(null); $model->create(null);
$return[$model->alias] = true;
if (!($model->set($data) && $model->validates($options))) { if (!($model->set($data) && $model->validates($options))) {
$validationErrors[$model->alias] = $model->validationErrors; $validationErrors[$model->alias] = $model->validationErrors;
$return[$model->alias] = false; $return[$model->alias] = false;
} else {
$return[$model->alias] = true;
} }
$data = $model->data; $data = $model->data;
if (!empty($options['deep']) && isset($data[$model->alias])) { if (!empty($options['deep']) && isset($data[$model->alias])) {
@ -156,11 +155,7 @@ class ModelValidator implements ArrayAccess, IteratorAggregate, Countable {
$data[$association] = $model->{$association}->data[$model->{$association}->alias]; $data[$association] = $model->{$association}->data[$model->{$association}->alias];
} }
if (is_array($validates)) { if (is_array($validates)) {
if (in_array(false, Hash::flatten($validates), true)) { $validates = !in_array(false, Hash::flatten($validates), true);
$validates = false;
} else {
$validates = true;
}
} }
$return[$association] = $validates; $return[$association] = $validates;
} elseif ($associations[$association] === 'hasMany') { } elseif ($associations[$association] === 'hasMany') {
@ -231,10 +226,7 @@ class ModelValidator implements ArrayAccess, IteratorAggregate, Countable {
if (!$options['atomic']) { if (!$options['atomic']) {
return $return; return $return;
} }
if (empty($model->validationErrors)) { return empty($model->validationErrors);
return true;
}
return false;
} }
/** /**
@ -322,9 +314,10 @@ class ModelValidator implements ArrayAccess, IteratorAggregate, Countable {
*/ */
public function getField($name = null) { public function getField($name = null) {
$this->_parseRules(); $this->_parseRules();
if ($name !== null && !empty($this->_fields[$name])) { if ($name !== null) {
return $this->_fields[$name]; if (!empty($this->_fields[$name])) {
} elseif ($name !== null) { return $this->_fields[$name];
}
return null; return null;
} }
return $this->_fields; return $this->_fields;
@ -401,18 +394,17 @@ class ModelValidator implements ArrayAccess, IteratorAggregate, Countable {
unset($fieldList); unset($fieldList);
$validateList = array(); $validateList = array();
if (!empty($whitelist)) { if (empty($whitelist)) {
$this->validationErrors = array();
foreach ((array)$whitelist as $f) {
if (!empty($this->_fields[$f])) {
$validateList[$f] = $this->_fields[$f];
}
}
} else {
return $this->_fields; return $this->_fields;
} }
$this->validationErrors = array();
foreach ((array)$whitelist as $f) {
if (!empty($this->_fields[$f])) {
$validateList[$f] = $this->_fields[$f];
}
}
return $validateList; return $validateList;
} }
@ -443,9 +435,6 @@ class ModelValidator implements ArrayAccess, IteratorAggregate, Countable {
$newData[] = $row[$join]; $newData[] = $row[$join];
} }
} }
if (empty($newData)) {
continue;
}
foreach ($newData as $data) { foreach ($newData as $data) {
$data[$model->hasAndBelongsToMany[$assoc]['foreignKey']] = $model->id; $data[$model->hasAndBelongsToMany[$assoc]['foreignKey']] = $model->id;
$model->{$join}->create($data); $model->{$join}->create($data);