Skip to content

Commit

Permalink
Add a way to restore 3.x behavior for last=true
Browse files Browse the repository at this point in the history
  • Loading branch information
dereuromark committed Aug 30, 2020
1 parent fee4015 commit 8dc0da2
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
28 changes: 27 additions & 1 deletion src/Validation/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand All @@ -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.
*
Expand Down Expand Up @@ -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 */
Expand Down Expand Up @@ -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,
];
Expand Down
22 changes: 22 additions & 0 deletions tests/TestCase/Validation/ValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down Expand Up @@ -2155,6 +2176,7 @@ public function testDebugInfo()
'published' => Validator::EMPTY_STRING,
],
'_useI18n' => true,
'_stopOnFailure' => false,
];
$this->assertEquals($expected, $result);
}
Expand Down

0 comments on commit 8dc0da2

Please sign in to comment.