Browse Source

Merge branch '2.2-validatorrules' into 2.2

Ceeram 13 years ago
parent
commit
2f5f1b28bc

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

@@ -3396,7 +3396,7 @@ class Model extends Object implements CakeEventListener {
 			return $this->_validator = $instance;
 		}
 
-		if (is_null($instance)) {
+		if (empty($this->_validator) && is_null($instance)) {
 			$this->_validator = new ModelValidator($this);
 		}
 

+ 1 - 0
lib/Cake/Model/ModelValidator.php

@@ -304,6 +304,7 @@ class ModelValidator implements ArrayAccess, IteratorAggregate, Countable {
  * @return CakeValidationSet|array
  */
 	public function getField($name = null) {
+		$this->_parseRules();
 		if ($name !== null && !empty($this->_fields[$name])) {
 			return $this->_fields[$name];
 		} elseif ($name !== null) {

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

@@ -2088,6 +2088,24 @@ class ModelValidationTest extends BaseModelTest {
 	}
 
 /**
+ * Test that rules are parsed correctly when calling getField()
+ *
+ * @return void
+ */
+	public function testValidator() {
+		$TestModel = new Article();
+		$Validator = $TestModel->validator();
+
+		$result = $Validator->getField();
+		$expected = array('user_id', 'title', 'body');
+		$this->assertEquals($expected, array_keys($result));
+		$this->assertTrue($result['user_id'] instanceof CakeValidationSet);
+
+		$result = $TestModel->validator()->getField('title');
+		$this->assertTrue($result instanceof CakeValidationSet);
+	}
+
+/**
  * Tests that altering data in a beforeValidate callback will lead to saving those
  * values in database, this time with belongsTo associations
  *