ソースを参照

Overwrite invalidate to allow last => true

euromark 13 年 前
コミット
a8f3d8c178
2 ファイル変更69 行追加1 行削除
  1. 18 1
      Model/MyModel.php
  2. 51 0
      Test/Case/Model/MyModelTest.php

+ 18 - 1
Model/MyModel.php

@@ -832,6 +832,23 @@ class MyModel extends Model {
 
 
 /** Validation Functions **/
 /** Validation Functions **/
 
 
+	/**
+	 * Overwrite invalidate to allow last => true
+	 *
+	 * @param string $field The name of the field to invalidate
+	 * @param mixed $value Name of validation rule that was not failed, or validation message to
+	 *    be returned. If no validation key is provided, defaults to true.
+	 * @param boolean $last If this should be the last validation check for this validation run
+	 * @return void
+	 */
+	public function invalidate($field, $value = true, $last = false) {
+		parent::invalidate($field, $value);
+		if (!$last) {
+			return;
+		}
+
+		$this->validator()->remove($field);
+	}
 
 
 	/**
 	/**
 	 * validates a primary or foreign key depending on the current schema data for this field
 	 * validates a primary or foreign key depending on the current schema data for this field
@@ -1615,7 +1632,7 @@ class MyModel extends Model {
 	 * @deprecated use generateTreeList instead
 	 * @deprecated use generateTreeList instead
 	 * 2009-08-12 ms
 	 * 2009-08-12 ms
 	 */
 	 */
-	public function _generateNestedList($cats, $indent, $level = 0) {
+	public function _generateNestedList($cats, $indent = '--', $level = 0) {
 		static $list = array();
 		static $list = array();
 		$c = count($cats);
 		$c = count($cats);
 		for ($i = 0; $i < $c; $i++) {
 		for ($i = 0; $i < $c; $i++) {

+ 51 - 0
Test/Case/Model/MyModelTest.php

@@ -136,6 +136,57 @@ class MyModelTest extends MyCakeTestCase {
 		$this->assertEquals(1, $is);
 		$this->assertEquals(1, $is);
 	}
 	}
 
 
+	/**
+	 * Test that 2.x invalidates() can behave like 1.x invalidates()
+	 * and that you are able to abort on single errors (similar to using last=>true)
+	 *
+	 * 2013-02-19 ms
+	 */
+	public function testInvalidates() {
+		$TestModel = new AppTestModel();
+
+		$TestModel->validate = array(
+			'title' => array(
+				'tooShort' => array(
+					'rule' => array('minLength', 50),
+					'last' => false
+				),
+				'onlyLetters' => array('rule' => '/^[a-z]+$/i')
+			),
+		);
+		$data = array(
+			'title' => 'I am a short string'
+		);
+		$TestModel->create($data);
+		$TestModel->invalidate('title', 'someCustomMessage');
+
+		$result = $TestModel->validates();
+		$this->assertFalse($result);
+
+		$result = $TestModel->validationErrors;
+		$expected = array(
+			'title' => array('someCustomMessage', 'tooShort', 'onlyLetters')
+		);
+		$this->assertEquals($expected, $result);
+		$result = $TestModel->validationErrors;
+		$this->assertEquals($expected, $result);
+
+		// invalidate a field with 'last' => true and stop further validation for this field
+		$TestModel->create($data);
+
+		$TestModel->invalidate('title', 'someCustomMessage', true);
+
+		$result = $TestModel->validates();
+		$this->assertFalse($result);
+		$result = $TestModel->validationErrors;
+		$expected = array(
+			'title' => array('someCustomMessage')
+		);
+		$this->assertEquals($expected, $result);
+		$result = $TestModel->validationErrors;
+		$this->assertEquals($expected, $result);
+	}
+
 	public function testValidateIdentical() {
 	public function testValidateIdentical() {
 		$this->out($this->_header(__FUNCTION__));
 		$this->out($this->_header(__FUNCTION__));
 		$this->App->data = array($this->App->alias=>array('y'=>'efg'));
 		$this->App->data = array($this->App->alias=>array('y'=>'efg'));