Browse files

Merge pull request #1000 from dogmatic69/validation-simplify

Validation simplify

Simplify internals in Model/Validation
  • Loading branch information...
2 parents 8b0a7ee + 03882f4 commit 38c592928dfeb1e4dc714d3e646e5e7e77c19750 @markstory markstory committed Dec 8, 2012
Showing with 14 additions and 25 deletions.
  1. +14 −25 lib/Cake/Model/ModelValidator.php
View
39 lib/Cake/Model/ModelValidator.php
@@ -130,11 +130,10 @@ public function validateAssociated(&$data, $options = array()) {
$options = array_merge(array('atomic' => true, 'deep' => false), $options);
$model->validationErrors = $validationErrors = $return = array();
$model->create(null);
+ $return[$model->alias] = true;
if (!($model->set($data) && $model->validates($options))) {
$validationErrors[$model->alias] = $model->validationErrors;
$return[$model->alias] = false;
- } else {
- $return[$model->alias] = true;
}
$data = $model->data;
if (!empty($options['deep']) && isset($data[$model->alias])) {
@@ -156,11 +155,7 @@ public function validateAssociated(&$data, $options = array()) {
$data[$association] = $model->{$association}->data[$model->{$association}->alias];
}
if (is_array($validates)) {
- if (in_array(false, Hash::flatten($validates), true)) {
- $validates = false;
- } else {
- $validates = true;
- }
+ $validates = !in_array(false, Hash::flatten($validates), true);
}
$return[$association] = $validates;
} elseif ($associations[$association] === 'hasMany') {
@@ -231,10 +226,7 @@ public function validateMany(&$data, $options = array()) {
if (!$options['atomic']) {
return $return;
}
- if (empty($model->validationErrors)) {
- return true;
- }
- return false;
+ return empty($model->validationErrors);
}
/**
@@ -322,9 +314,10 @@ public function getMethods() {
*/
public function getField($name = null) {
$this->_parseRules();
- if ($name !== null && !empty($this->_fields[$name])) {
- return $this->_fields[$name];
- } elseif ($name !== null) {
+ if ($name !== null) {
+ if (!empty($this->_fields[$name])) {
+ return $this->_fields[$name];
+ }
return null;
}
return $this->_fields;
@@ -401,16 +394,15 @@ protected function _validationList($fieldList = array()) {
unset($fieldList);
$validateList = array();
- if (!empty($whitelist)) {
- $this->validationErrors = array();
+ if (empty($whitelist)) {
+ return $this->_fields;
+ }
- foreach ((array)$whitelist as $f) {
- if (!empty($this->_fields[$f])) {
- $validateList[$f] = $this->_fields[$f];
- }
+ $this->validationErrors = array();
+ foreach ((array)$whitelist as $f) {
+ if (!empty($this->_fields[$f])) {
+ $validateList[$f] = $this->_fields[$f];
}
- } else {
- return $this->_fields;
}
return $validateList;
@@ -443,9 +435,6 @@ protected function _validateWithModels($options) {
$newData[] = $row[$join];
}
}
- if (empty($newData)) {
- continue;
- }
foreach ($newData as $data) {
$data[$model->hasAndBelongsToMany[$assoc]['foreignKey']] = $model->id;
$model->{$join}->create($data);

0 comments on commit 38c5929

Please sign in to comment.