Skip to content
Permalink
Browse files

Stopping validation for a field if the rule is marked as 'last'

  • Loading branch information...
lorenzo committed Nov 14, 2013
1 parent 82aa396 commit 37dc016fefc0929503c94284cf1ba9869e4b9b06
Showing with 26 additions and 2 deletions.
  1. +2 −2 Cake/ORM/Validation/ValidationRule.php
  2. +4 −0 Cake/ORM/Validator.php
  3. +20 −0 Cake/Test/TestCase/ORM/ValidatorTest.php
@@ -45,7 +45,7 @@ class ValidationRule {
*
* @var boolean
*/
protected $_last = true;
protected $_last = false;
/**
* The 'message' key
@@ -99,7 +99,7 @@ public function skip() {
* @return boolean
*/
public function isLast() {
return (bool)$this->last;
return (bool)$this->_last;
}
/**
@@ -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;
}
@@ -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.
You can’t perform that action at this time.