From 24abed969adb9423c8e77d43bc1d0bd9a1efb28f Mon Sep 17 00:00:00 2001 From: dogmatic69 Date: Mon, 3 Dec 2012 15:51:20 +0000 Subject: [PATCH 1/2] simplify validation code --- lib/Cake/Model/ModelValidator.php | 41 ++++++++++++------------------- 1 file changed, 16 insertions(+), 25 deletions(-) diff --git a/lib/Cake/Model/ModelValidator.php b/lib/Cake/Model/ModelValidator.php index 9cc440cf0..40cd4c44e 100644 --- a/lib/Cake/Model/ModelValidator.php +++ b/lib/Cake/Model/ModelValidator.php @@ -130,11 +130,10 @@ class ModelValidator implements ArrayAccess, IteratorAggregate, Countable { $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,10 +155,9 @@ class ModelValidator implements ArrayAccess, IteratorAggregate, Countable { $data[$association] = $model->{$association}->data[$model->{$association}->alias]; } if (is_array($validates)) { + $validates = true; if (in_array(false, Hash::flatten($validates), true)) { $validates = false; - } else { - $validates = true; } } $return[$association] = $validates; @@ -219,11 +217,10 @@ class ModelValidator implements ArrayAccess, IteratorAggregate, Countable { $validates = $model->set($record) && $model->validates($options); $data[$key] = $model->data; } + $validates = true; if ($validates === false || (is_array($validates) && in_array(false, Hash::flatten($validates), true))) { $validationErrors[$key] = $model->validationErrors; $validates = false; - } else { - $validates = true; } $return[$key] = $validates; } @@ -231,10 +228,7 @@ class ModelValidator implements ArrayAccess, IteratorAggregate, Countable { if (!$options['atomic']) { return $return; } - if (empty($model->validationErrors)) { - return true; - } - return false; + return empty($model->validationErrors); } /** @@ -322,9 +316,10 @@ class ModelValidator implements ArrayAccess, IteratorAggregate, Countable { */ 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,18 +396,17 @@ class ModelValidator implements ArrayAccess, IteratorAggregate, Countable { unset($fieldList); $validateList = array(); - if (!empty($whitelist)) { - $this->validationErrors = array(); - - foreach ((array)$whitelist as $f) { - if (!empty($this->_fields[$f])) { - $validateList[$f] = $this->_fields[$f]; - } - } - } else { + if (empty($whitelist)) { return $this->_fields; } + $this->validationErrors = array(); + foreach ((array)$whitelist as $f) { + if (!empty($this->_fields[$f])) { + $validateList[$f] = $this->_fields[$f]; + } + } + return $validateList; } @@ -443,9 +437,6 @@ class ModelValidator implements ArrayAccess, IteratorAggregate, Countable { $newData[] = $row[$join]; } } - if (empty($newData)) { - continue; - } foreach ($newData as $data) { $data[$model->hasAndBelongsToMany[$assoc]['foreignKey']] = $model->id; $model->{$join}->create($data); From 03882f4b55a3eb190f17e0ae58cd69229c8ec7fc Mon Sep 17 00:00:00 2001 From: dogmatic69 Date: Tue, 4 Dec 2012 01:58:57 +0000 Subject: [PATCH 2/2] fix up the fails --- lib/Cake/Model/ModelValidator.php | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/lib/Cake/Model/ModelValidator.php b/lib/Cake/Model/ModelValidator.php index 40cd4c44e..462915916 100644 --- a/lib/Cake/Model/ModelValidator.php +++ b/lib/Cake/Model/ModelValidator.php @@ -155,10 +155,7 @@ class ModelValidator implements ArrayAccess, IteratorAggregate, Countable { $data[$association] = $model->{$association}->data[$model->{$association}->alias]; } if (is_array($validates)) { - $validates = true; - if (in_array(false, Hash::flatten($validates), true)) { - $validates = false; - } + $validates = !in_array(false, Hash::flatten($validates), true); } $return[$association] = $validates; } elseif ($associations[$association] === 'hasMany') { @@ -217,10 +214,11 @@ class ModelValidator implements ArrayAccess, IteratorAggregate, Countable { $validates = $model->set($record) && $model->validates($options); $data[$key] = $model->data; } - $validates = true; if ($validates === false || (is_array($validates) && in_array(false, Hash::flatten($validates), true))) { $validationErrors[$key] = $model->validationErrors; $validates = false; + } else { + $validates = true; } $return[$key] = $validates; }