Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Optimized the validation processes when 'on' keys aren't used.

  • Loading branch information...
commit 0141dadec7d102fe867719b48ca5f23d7724e7e1 1 parent 175d148
Frank de Graaf Phally authored
126 cake/libs/model/model.php
View
@@ -2530,7 +2530,7 @@ function invalidFields($options = array()) {
}
$Validation =& Validation::getInstance();
- $exists = $this->exists();
+ $exists = null;
$_validate = $this->validate;
$whitelist = $this->whitelist;
@@ -2573,72 +2573,76 @@ function invalidFields($options = array()) {
$message = __('This field cannot be left blank', true);
}
- if (
- empty($validator['on']) || ($validator['on'] == 'create' &&
- !$exists) || ($validator['on'] == 'update' && $exists
- )) {
- $required = (
- (!isset($data[$fieldName]) && $validator['required'] === true) ||
- (
- isset($data[$fieldName]) && (empty($data[$fieldName]) &&
- !is_numeric($data[$fieldName])) && $validator['allowEmpty'] === false
- )
- );
+ if (!empty($validator['on'])) {
+ if ($exists === null) {
+ $exists = $this->exists();
+ }
+ if (($validator['on'] == 'create' && $exists) || ($validator['on'] == 'update' && !$exists)) {
+ continue;
+ }
+ }
- if ($required) {
- $this->invalidate($fieldName, $message);
- if ($validator['last']) {
- break;
- }
- } elseif (array_key_exists($fieldName, $data)) {
- if (empty($data[$fieldName]) && $data[$fieldName] != '0' && $validator['allowEmpty'] === true) {
- break;
- }
- if (is_array($validator['rule'])) {
- $rule = $validator['rule'][0];
- unset($validator['rule'][0]);
- $ruleParams = array_merge(array($data[$fieldName]), array_values($validator['rule']));
- } else {
- $rule = $validator['rule'];
- $ruleParams = array($data[$fieldName]);
- }
+ $required = (
+ (!isset($data[$fieldName]) && $validator['required'] === true) ||
+ (
+ isset($data[$fieldName]) && (empty($data[$fieldName]) &&
+ !is_numeric($data[$fieldName])) && $validator['allowEmpty'] === false
+ )
+ );
- $valid = true;
-
- if (in_array(strtolower($rule), $methods)) {
- $ruleParams[] = $validator;
- $ruleParams[0] = array($fieldName => $ruleParams[0]);
- $valid = $this->dispatchMethod($rule, $ruleParams);
- } elseif (in_array($rule, $behaviorMethods) || in_array(strtolower($rule), $behaviorMethods)) {
- $ruleParams[] = $validator;
- $ruleParams[0] = array($fieldName => $ruleParams[0]);
- $valid = $this->Behaviors->dispatchMethod($this, $rule, $ruleParams);
- } elseif (method_exists($Validation, $rule)) {
- $valid = $Validation->dispatchMethod($rule, $ruleParams);
- } elseif (!is_array($validator['rule'])) {
- $valid = preg_match($rule, $data[$fieldName]);
- } elseif (Configure::read('debug') > 0) {
- trigger_error(sprintf(__('Could not find validation handler %s for %s', true), $rule, $fieldName), E_USER_WARNING);
- }
+ if ($required) {
+ $this->invalidate($fieldName, $message);
+ if ($validator['last']) {
+ break;
+ }
+ } elseif (array_key_exists($fieldName, $data)) {
+ if (empty($data[$fieldName]) && $data[$fieldName] != '0' && $validator['allowEmpty'] === true) {
+ break;
+ }
+ if (is_array($validator['rule'])) {
+ $rule = $validator['rule'][0];
+ unset($validator['rule'][0]);
+ $ruleParams = array_merge(array($data[$fieldName]), array_values($validator['rule']));
+ } else {
+ $rule = $validator['rule'];
+ $ruleParams = array($data[$fieldName]);
+ }
- if (!$valid || (is_string($valid) && strlen($valid) > 0)) {
- if (is_string($valid) && strlen($valid) > 0) {
- $validator['message'] = $valid;
- } elseif (!isset($validator['message'])) {
- if (is_string($index)) {
- $validator['message'] = $index;
- } elseif (is_numeric($index) && count($ruleSet) > 1) {
- $validator['message'] = $index + 1;
- } else {
- $validator['message'] = $message;
- }
- }
- $this->invalidate($fieldName, $validator['message']);
+ $valid = true;
+
+ if (in_array(strtolower($rule), $methods)) {
+ $ruleParams[] = $validator;
+ $ruleParams[0] = array($fieldName => $ruleParams[0]);
+ $valid = $this->dispatchMethod($rule, $ruleParams);
+ } elseif (in_array($rule, $behaviorMethods) || in_array(strtolower($rule), $behaviorMethods)) {
+ $ruleParams[] = $validator;
+ $ruleParams[0] = array($fieldName => $ruleParams[0]);
+ $valid = $this->Behaviors->dispatchMethod($this, $rule, $ruleParams);
+ } elseif (method_exists($Validation, $rule)) {
+ $valid = $Validation->dispatchMethod($rule, $ruleParams);
+ } elseif (!is_array($validator['rule'])) {
+ $valid = preg_match($rule, $data[$fieldName]);
+ } elseif (Configure::read('debug') > 0) {
+ trigger_error(sprintf(__('Could not find validation handler %s for %s', true), $rule, $fieldName), E_USER_WARNING);
+ }
- if ($validator['last']) {
- break;
+ if (!$valid || (is_string($valid) && strlen($valid) > 0)) {
+ if (is_string($valid) && strlen($valid) > 0) {
+ $validator['message'] = $valid;
+ } elseif (!isset($validator['message'])) {
+ if (is_string($index)) {
+ $validator['message'] = $index;
+ } elseif (is_numeric($index) && count($ruleSet) > 1) {
+ $validator['message'] = $index + 1;
+ } else {
+ $validator['message'] = $message;
}
}
+ $this->invalidate($fieldName, $validator['message']);
+
+ if ($validator['last']) {
+ break;
+ }
}
}
}
14 cake/tests/cases/libs/model/model_validation.test.php
View
@@ -734,6 +734,11 @@ function testStateValidation() {
'rule' => 'notEmpty',
'on' => 'create'
)
+ ),
+ 'published' => array(
+ 'notempty' => array(
+ 'rule' => 'notEmpty',
+ )
)
);
@@ -745,13 +750,22 @@ function testStateValidation() {
$Article->set($data);
$this->assertTrue($Article->validates());
+ $Article->data['Article']['published'] = null;
+ $this->assertFalse($Article->validates());
+
unset($data['Article']['id']);
+ $Article->data['Article']['published'] = '1';
$Article->validate = array(
'title' => array(
'notempty' => array(
'rule' => 'notEmpty',
'on' => 'update'
)
+ ),
+ 'published' => array(
+ 'notempty' => array(
+ 'rule' => 'notEmpty',
+ )
)
);
Please sign in to comment.
Something went wrong with that request. Please try again.