Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

simplify validation code

  • Loading branch information...
commit 24abed969adb9423c8e77d43bc1d0bd9a1efb28f 1 parent 9bcc05b
@dogmatic69 dogmatic69 authored
Showing with 15 additions and 24 deletions.
  1. +15 −24 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,10 +155,9 @@ public function validateAssociated(&$data, $options = array()) {
$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 @@ public function validateMany(&$data, $options = array()) {
$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 @@ 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 +316,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 +396,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 +437,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);
Please sign in to comment.
Something went wrong with that request. Please try again.