Skip to content
This repository
Browse code

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

  • Loading branch information...
commit 0141dadec7d102fe867719b48ca5f23d7724e7e1 1 parent 175d148
Frank de Graaf authored October 31, 2011
126  cake/libs/model/model.php
@@ -2530,7 +2530,7 @@ function invalidFields($options = array()) {
2530 2530
 		}
2531 2531
 
2532 2532
 		$Validation =& Validation::getInstance();
2533  
-		$exists = $this->exists();
  2533
+		$exists = null;
2534 2534
 
2535 2535
 		$_validate = $this->validate;
2536 2536
 		$whitelist = $this->whitelist;
@@ -2573,72 +2573,76 @@ function invalidFields($options = array()) {
2573 2573
 					$message = __('This field cannot be left blank', true);
2574 2574
 				}
2575 2575
 
2576  
-				if (
2577  
-					empty($validator['on']) || ($validator['on'] == 'create' &&
2578  
-					!$exists) || ($validator['on'] == 'update' && $exists
2579  
-				)) {
2580  
-					$required = (
2581  
-						(!isset($data[$fieldName]) && $validator['required'] === true) ||
2582  
-						(
2583  
-							isset($data[$fieldName]) && (empty($data[$fieldName]) &&
2584  
-							!is_numeric($data[$fieldName])) && $validator['allowEmpty'] === false
2585  
-						)
2586  
-					);
  2576
+				if (!empty($validator['on'])) {
  2577
+					if ($exists === null) {
  2578
+						$exists = $this->exists();
  2579
+					}
  2580
+					if (($validator['on'] == 'create' && $exists) || ($validator['on'] == 'update' && !$exists)) {
  2581
+						continue;
  2582
+					}
  2583
+				}
2587 2584
 
2588  
-					if ($required) {
2589  
-						$this->invalidate($fieldName, $message);
2590  
-						if ($validator['last']) {
2591  
-							break;
2592  
-						}
2593  
-					} elseif (array_key_exists($fieldName, $data)) {
2594  
-						if (empty($data[$fieldName]) && $data[$fieldName] != '0' && $validator['allowEmpty'] === true) {
2595  
-							break;
2596  
-						}
2597  
-						if (is_array($validator['rule'])) {
2598  
-							$rule = $validator['rule'][0];
2599  
-							unset($validator['rule'][0]);
2600  
-							$ruleParams = array_merge(array($data[$fieldName]), array_values($validator['rule']));
2601  
-						} else {
2602  
-							$rule = $validator['rule'];
2603  
-							$ruleParams = array($data[$fieldName]);
2604  
-						}
  2585
+				$required = (
  2586
+					(!isset($data[$fieldName]) && $validator['required'] === true) ||
  2587
+					(
  2588
+						isset($data[$fieldName]) && (empty($data[$fieldName]) &&
  2589
+						!is_numeric($data[$fieldName])) && $validator['allowEmpty'] === false
  2590
+					)
  2591
+				);
2605 2592
 
2606  
-						$valid = true;
2607  
-
2608  
-						if (in_array(strtolower($rule), $methods)) {
2609  
-							$ruleParams[] = $validator;
2610  
-							$ruleParams[0] = array($fieldName => $ruleParams[0]);
2611  
-							$valid = $this->dispatchMethod($rule, $ruleParams);
2612  
-						} elseif (in_array($rule, $behaviorMethods) || in_array(strtolower($rule), $behaviorMethods)) {
2613  
-							$ruleParams[] = $validator;
2614  
-							$ruleParams[0] = array($fieldName => $ruleParams[0]);
2615  
-							$valid = $this->Behaviors->dispatchMethod($this, $rule, $ruleParams);
2616  
-						} elseif (method_exists($Validation, $rule)) {
2617  
-							$valid = $Validation->dispatchMethod($rule, $ruleParams);
2618  
-						} elseif (!is_array($validator['rule'])) {
2619  
-							$valid = preg_match($rule, $data[$fieldName]);
2620  
-						} elseif (Configure::read('debug') > 0) {
2621  
-							trigger_error(sprintf(__('Could not find validation handler %s for %s', true), $rule, $fieldName), E_USER_WARNING);
2622  
-						}
  2593
+				if ($required) {
  2594
+					$this->invalidate($fieldName, $message);
  2595
+					if ($validator['last']) {
  2596
+						break;
  2597
+					}
  2598
+				} elseif (array_key_exists($fieldName, $data)) {
  2599
+					if (empty($data[$fieldName]) && $data[$fieldName] != '0' && $validator['allowEmpty'] === true) {
  2600
+						break;
  2601
+					}
  2602
+					if (is_array($validator['rule'])) {
  2603
+						$rule = $validator['rule'][0];
  2604
+						unset($validator['rule'][0]);
  2605
+						$ruleParams = array_merge(array($data[$fieldName]), array_values($validator['rule']));
  2606
+					} else {
  2607
+						$rule = $validator['rule'];
  2608
+						$ruleParams = array($data[$fieldName]);
  2609
+					}
2623 2610
 
2624  
-						if (!$valid || (is_string($valid) && strlen($valid) > 0)) {
2625  
-							if (is_string($valid) && strlen($valid) > 0) {
2626  
-								$validator['message'] = $valid;
2627  
-							} elseif (!isset($validator['message'])) {
2628  
-								if (is_string($index)) {
2629  
-									$validator['message'] = $index;
2630  
-								} elseif (is_numeric($index) && count($ruleSet) > 1) {
2631  
-									$validator['message'] = $index + 1;
2632  
-								} else {
2633  
-									$validator['message'] = $message;
2634  
-								}
2635  
-							}
2636  
-							$this->invalidate($fieldName, $validator['message']);
  2611
+					$valid = true;
  2612
+
  2613
+					if (in_array(strtolower($rule), $methods)) {
  2614
+						$ruleParams[] = $validator;
  2615
+						$ruleParams[0] = array($fieldName => $ruleParams[0]);
  2616
+						$valid = $this->dispatchMethod($rule, $ruleParams);
  2617
+					} elseif (in_array($rule, $behaviorMethods) || in_array(strtolower($rule), $behaviorMethods)) {
  2618
+						$ruleParams[] = $validator;
  2619
+						$ruleParams[0] = array($fieldName => $ruleParams[0]);
  2620
+						$valid = $this->Behaviors->dispatchMethod($this, $rule, $ruleParams);
  2621
+					} elseif (method_exists($Validation, $rule)) {
  2622
+						$valid = $Validation->dispatchMethod($rule, $ruleParams);
  2623
+					} elseif (!is_array($validator['rule'])) {
  2624
+						$valid = preg_match($rule, $data[$fieldName]);
  2625
+					} elseif (Configure::read('debug') > 0) {
  2626
+						trigger_error(sprintf(__('Could not find validation handler %s for %s', true), $rule, $fieldName), E_USER_WARNING);
  2627
+					}
2637 2628
 
2638  
-							if ($validator['last']) {
2639  
-								break;
  2629
+					if (!$valid || (is_string($valid) && strlen($valid) > 0)) {
  2630
+						if (is_string($valid) && strlen($valid) > 0) {
  2631
+							$validator['message'] = $valid;
  2632
+						} elseif (!isset($validator['message'])) {
  2633
+							if (is_string($index)) {
  2634
+								$validator['message'] = $index;
  2635
+							} elseif (is_numeric($index) && count($ruleSet) > 1) {
  2636
+								$validator['message'] = $index + 1;
  2637
+							} else {
  2638
+								$validator['message'] = $message;
2640 2639
 							}
2641 2640
 						}
  2641
+						$this->invalidate($fieldName, $validator['message']);
  2642
+
  2643
+						if ($validator['last']) {
  2644
+							break;
  2645
+						}
2642 2646
 					}
2643 2647
 				}
2644 2648
 			}
14  cake/tests/cases/libs/model/model_validation.test.php
@@ -734,6 +734,11 @@ function testStateValidation() {
734 734
 					'rule' => 'notEmpty',
735 735
 					'on' => 'create'
736 736
 				)
  737
+			),
  738
+			'published' => array(
  739
+				'notempty' => array(
  740
+					'rule' => 'notEmpty',
  741
+				)
737 742
 			)
738 743
 		);
739 744
 
@@ -745,13 +750,22 @@ function testStateValidation() {
745 750
 		$Article->set($data);
746 751
 		$this->assertTrue($Article->validates());
747 752
 
  753
+		$Article->data['Article']['published'] = null;
  754
+		$this->assertFalse($Article->validates());
  755
+
748 756
 		unset($data['Article']['id']);
  757
+		$Article->data['Article']['published'] = '1';
749 758
 		$Article->validate = array(
750 759
 			'title' => array(
751 760
 				'notempty' => array(
752 761
 					'rule' => 'notEmpty',
753 762
 					'on' => 'update'
754 763
 				)
  764
+			),
  765
+			'published' => array(
  766
+				'notempty' => array(
  767
+					'rule' => 'notEmpty',
  768
+				)
755 769
 			)
756 770
 		);
757 771
 

0 notes on commit 0141dad

Please sign in to comment.
Something went wrong with that request. Please try again.