Browse Source

Merge branch 'master' of github.com:cakephp/cakephp

mark_story 13 years ago
parent
commit
7966c00700

+ 22 - 4
lib/Cake/Model/ModelValidator.php

@@ -64,6 +64,20 @@ class ModelValidator implements ArrayAccess, IteratorAggregate, Countable {
 	protected $_methods = array();
 
 /**
+ * Holds the available custom callback methods from the model
+ *
+ * @var array
+ */
+	protected $_modelMethods = array();
+
+/**
+ * Holds the list of behavior names that were attached when this object was created
+ *
+ * @var array
+ */
+	protected $_behaviors = array();
+
+/**
  * Constructor
  *
  * @param Model $Model A reference to the Model the Validator is attached to
@@ -280,15 +294,19 @@ class ModelValidator implements ArrayAccess, IteratorAggregate, Countable {
  * @return array List of callables to be used as validation methods
  */
 	public function getMethods() {
-		if (!empty($this->_methods)) {
+		$behaviors = $this->_model->Behaviors->enabled();
+		if (!empty($this->_methods) && $behaviors === $this->_behaviors) {
 			return $this->_methods;
 		}
+		$this->_behaviors = $behaviors;
 
-		$methods = array();
-		foreach (get_class_methods($this->_model) as $method) {
-			$methods[strtolower($method)] = array($this->_model, $method);
+		if (empty($this->_modelMethods)) {
+			foreach (get_class_methods($this->_model) as $method) {
+				$this->_modelMethods[strtolower($method)] = array($this->_model, $method);
+			}
 		}
 
+		$methods = $this->_modelMethods;
 		foreach (array_keys($this->_model->Behaviors->methods()) as $method) {
 			$methods += array(strtolower($method) => array($this->_model, $method));
 		}

+ 11 - 0
lib/Cake/Model/Validator/CakeValidationRule.php

@@ -283,6 +283,17 @@ class CakeValidationRule {
 	}
 
 /**
+ * Resets interal state for this rule, by default it will become valid
+ * and it will set isUpdate() to false
+ *
+ * @return void
+ **/
+	public function reset() {
+		$this->_valid = true;
+		$this->_recordExists = false;
+	}
+
+/**
  * Returns passed options for this rule
  *
  * @return array

+ 12 - 0
lib/Cake/Model/Validator/CakeValidationSet.php

@@ -117,6 +117,7 @@ class CakeValidationSet implements ArrayAccess, IteratorAggregate, Countable {
  * @return array list of validation errors for this field
  */
 	public function validate($data, $isUpdate = false) {
+		$this->reset();
 		$errors = array();
 		foreach ($this->getRules() as $name => $rule) {
 			$rule->isUpdate($isUpdate);
@@ -144,6 +145,17 @@ class CakeValidationSet implements ArrayAccess, IteratorAggregate, Countable {
 	}
 
 /**
+ * Resets interal state for all validation rules in this set
+ *
+ * @return void
+ **/
+	public function reset() {
+		foreach ($this->getRules() as $rule) {
+			$rule->reset();
+		}
+	}
+
+/**
  * Gets a rule for a given name if exists
  *
  * @param string $name

+ 29 - 0
lib/Cake/Test/Case/Model/ModelValidationTest.php

@@ -1701,6 +1701,35 @@ class ModelValidationTest extends BaseModelTest {
 	}
 
 /**
+ *  Tests that methods are refreshed when the list of behaviors change
+ *
+ * @return void
+ */
+	public function testGetMethodsRefresh() {
+		$this->loadFixtures('Article', 'Comment');
+		$TestModel = new Article();
+		$Validator = $TestModel->validator();
+
+		$result = $Validator->getMethods();
+
+		$expected = array_map('strtolower', get_class_methods('Article'));
+		$this->assertEquals($expected, array_keys($result));
+
+		$TestModel->Behaviors->attach('Containable');
+		$newList = array(
+			'contain',
+			'resetbindings',
+			'containments',
+			'fielddependencies',
+			'containmentsmap'
+		);
+		$this->assertEquals(array_merge($expected, $newList), array_keys($Validator->getMethods()));
+
+		$TestModel->Behaviors->detach('Containable');
+		$this->assertEquals($expected, array_keys($Validator->getMethods()));
+	}
+
+/**
  * testSetValidationDomain method
  *
  * @return void

+ 1 - 5
lib/Cake/Test/Case/Model/ModelWriteTest.php

@@ -3242,7 +3242,6 @@ class ModelWriteTest extends BaseModelTest {
 				)
 			),
 			1 => array(
-				'body' => array('This field cannot be left blank'),
 				'Comment' => array(
 					0 => array(
 						'User' => array(
@@ -3687,9 +3686,6 @@ class ModelWriteTest extends BaseModelTest {
 		$expected = array(
 			0 => array(
 				'body' => array('This field cannot be left blank')
-			),
-			1 => array(
-				'body' => array('This field cannot be left blank')
 			)
 		);
 		$result = $TestModel->validationErrors;
@@ -3703,7 +3699,7 @@ class ModelWriteTest extends BaseModelTest {
 				)
 			),
 			array(
-				'Article' => array('id' => 2, 'body' => 'Same here'),
+				'Article' => array('id' => 2),
 				'Comment' => array(
 					array('comment' => '', 'published' => 'Y', 'user_id' => 2)
 				)

+ 1 - 1
lib/Cake/Test/Case/Model/models.php

@@ -276,7 +276,7 @@ class Article extends CakeTestModel {
 	public $validate = array(
 		'user_id' => 'numeric',
 		'title' => array('required' => false, 'rule' => 'notEmpty'),
-		'body' => 'notEmpty',
+		'body' => array('required' => false, 'rule' => 'notEmpty'),
 	);
 
 /**

+ 1 - 1
lib/Cake/View/View.php

@@ -919,7 +919,7 @@ class View extends Object {
 
 		include $this->__viewFile;
 
-		unset($this->_viewFile);
+		unset($this->__viewFile);
 		return ob_get_clean();
 	}