Skip to content

Commit

Permalink
Stopping validation for a field if the rule is marked as 'last'
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed Nov 20, 2013
1 parent 82aa396 commit 37dc016
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 2 deletions.
4 changes: 2 additions & 2 deletions Cake/ORM/Validation/ValidationRule.php
Expand Up @@ -45,7 +45,7 @@ class ValidationRule {
*
* @var boolean
*/
protected $_last = true;
protected $_last = false;

/**
* The 'message' key
Expand Down Expand Up @@ -99,7 +99,7 @@ public function skip() {
* @return boolean
*/
public function isLast() {
return (bool)$this->last;
return (bool)$this->_last;
}

/**
Expand Down
4 changes: 4 additions & 0 deletions Cake/ORM/Validator.php
Expand Up @@ -373,6 +373,10 @@ protected function _processRules(ValidationSet $rules, $value, $newRecord) {
if (is_string($result)) {
$errors[$name] = __d($this->_validationDomain, $result);
}

if ($rule->isLast()) {
break;
}
}
return $errors;
}
Expand Down
20 changes: 20 additions & 0 deletions Cake/Test/TestCase/ORM/ValidatorTest.php
Expand Up @@ -312,4 +312,24 @@ public function testMethodsWithExtraArguments() {
];
$this->assertEquals($expected, $errors);
}

/**
* Tests that setting last to a rule will stop validating the rest of the rules
*
* @return void
*/
public function testErrorsWithLastRule() {
$validator = new Validator;
$validator
->add('email', 'alpha', ['rule' => 'alphanumeric', 'last' => true])
->add('email', 'email', ['rule' => 'email', 'message' => 'Y u no write email?']);
$errors = $validator->errors(['email' => 'not an email!']);
$expected = [
'email' => [
'alpha' => 'The provided value is invalid'
]
];

$this->assertEquals($expected, $errors);
}
}

0 comments on commit 37dc016

Please sign in to comment.