Browse Source

Add a way to restore 3.x behavior for last=true

mscherer 5 years ago
parent
commit
8dc0da2587
2 changed files with 49 additions and 1 deletions
  1. 27 1
      src/Validation/Validator.php
  2. 22 0
      tests/TestCase/Validation/ValidatorTest.php

+ 27 - 1
src/Validation/Validator.php

@@ -181,6 +181,13 @@ class Validator implements ArrayAccess, IteratorAggregate, Countable
     protected $_allowEmptyFlags = [];
 
     /**
+     * Whether to apply last flag to generated rule(s).
+     *
+     * @var bool
+     */
+    protected $_stopOnFailure = false;
+
+    /**
      * Constructor
      */
     public function __construct()
@@ -190,6 +197,21 @@ class Validator implements ArrayAccess, IteratorAggregate, Countable
     }
 
     /**
+     * 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.
      *
      * @param array $data The data to be checked for errors
@@ -479,7 +501,10 @@ class Validator implements ArrayAccess, IteratorAggregate, Countable
 
         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 @@ class Validator implements ArrayAccess, IteratorAggregate, Countable
             '_allowEmptyMessages' => $this->_allowEmptyMessages,
             '_allowEmptyFlags' => $this->_allowEmptyFlags,
             '_useI18n' => $this->_useI18n,
+            '_stopOnFailure' => $this->_stopOnFailure,
             '_providers' => array_keys($this->_providers),
             '_fields' => $fields,
         ];

+ 22 - 0
tests/TestCase/Validation/ValidatorTest.php

@@ -1926,6 +1926,27 @@ class ValidatorTest extends TestCase
     }
 
     /**
+     * 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
      *
      * @return void
@@ -2155,6 +2176,7 @@ class ValidatorTest extends TestCase
                 'published' => Validator::EMPTY_STRING,
             ],
             '_useI18n' => true,
+            '_stopOnFailure' => false,
         ];
         $this->assertEquals($expected, $result);
     }