From 8dc0da258714d6b6c46ecaaf416896936a1dc78d Mon Sep 17 00:00:00 2001 From: mscherer Date: Sat, 29 Aug 2020 16:16:19 +0200 Subject: [PATCH] Add a way to restore 3.x behavior for last=true --- src/Validation/Validator.php | 28 ++++++++++++++++++++- tests/TestCase/Validation/ValidatorTest.php | 22 ++++++++++++++++ 2 files changed, 49 insertions(+), 1 deletion(-) diff --git a/src/Validation/Validator.php b/src/Validation/Validator.php index 8f5e9e1972d..948e0807b20 100644 --- a/src/Validation/Validator.php +++ b/src/Validation/Validator.php @@ -180,6 +180,13 @@ class Validator implements ArrayAccess, IteratorAggregate, Countable */ protected $_allowEmptyFlags = []; + /** + * Whether to apply last flag to generated rule(s). + * + * @var bool + */ + protected $_stopOnFailure = false; + /** * Constructor */ @@ -189,6 +196,21 @@ public function __construct() $this->_providers = self::$_defaultProviders; } + /** + * Whether to apply last flag to generated rule(s). + * The first failing one will abort then per rule. + * + * @param bool $stopOnFailure If to apply last flag. + * + * @return $this + */ + public function stopOnFailure(bool $stopOnFailure = true) + { + $this->_stopOnFailure = $stopOnFailure; + + return $this; + } + /** * Validates and returns an array of failed fields and their error messages. * @@ -479,7 +501,10 @@ public function add(string $field, $name, $rule = []) foreach ($rules as $name => $rule) { if (is_array($rule)) { - $rule += ['rule' => $name]; + $rule += [ + 'rule' => $name, + 'last' => $this->_stopOnFailure, + ]; } if (!is_string($name)) { /** @psalm-suppress PossiblyUndefinedMethod */ @@ -2715,6 +2740,7 @@ public function __debugInfo(): array '_allowEmptyMessages' => $this->_allowEmptyMessages, '_allowEmptyFlags' => $this->_allowEmptyFlags, '_useI18n' => $this->_useI18n, + '_stopOnFailure' => $this->_stopOnFailure, '_providers' => array_keys($this->_providers), '_fields' => $fields, ]; diff --git a/tests/TestCase/Validation/ValidatorTest.php b/tests/TestCase/Validation/ValidatorTest.php index 4426466893d..99debfc3862 100644 --- a/tests/TestCase/Validation/ValidatorTest.php +++ b/tests/TestCase/Validation/ValidatorTest.php @@ -1925,6 +1925,27 @@ public function testUsingClosureAsRule() $this->assertEquals($expected, $validator->validate(['name' => 'foo'])); } + /** + * Tests that setting last globally will stop validating the rest of the rules + * + * @return void + */ + public function testErrorsWithLastRuleGlobal(): void + { + $validator = new Validator(); + $validator->stopOnFailure() + ->notBlank('email', 'Fill something in!') + ->email('email', false, 'Y u no write email?'); + $errors = $validator->validate(['email' => '']); + $expected = [ + 'email' => [ + 'notBlank' => 'Fill something in!', + ], + ]; + + $this->assertEquals($expected, $errors); + } + /** * Tests that setting last to a rule will stop validating the rest of the rules * @@ -2155,6 +2176,7 @@ public function testDebugInfo() 'published' => Validator::EMPTY_STRING, ], '_useI18n' => true, + '_stopOnFailure' => false, ]; $this->assertEquals($expected, $result); }