Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[12.x] Stops validation if a custom rule specifies it #54635

Draft
wants to merge 3 commits into
base: 12.x
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/Illuminate/Contracts/Validation/Bailable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace Illuminate\Contracts\Validation;

interface Bailable
{
}
11 changes: 9 additions & 2 deletions src/Illuminate/Validation/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
use BadMethodCallException;
use Illuminate\Contracts\Container\Container;
use Illuminate\Contracts\Translation\Translator;
use Illuminate\Contracts\Validation\Bailable;
use Illuminate\Contracts\Validation\DataAwareRule;
use Illuminate\Contracts\Validation\ImplicitRule;
use Illuminate\Contracts\Validation\Rule as RuleContract;
use Illuminate\Contracts\Validation\ValidationRule;
use Illuminate\Contracts\Validation\Validator as ValidatorContract;
use Illuminate\Contracts\Validation\ValidatorAwareRule;
use Illuminate\Support\Arr;
Expand Down Expand Up @@ -482,7 +484,7 @@ public function passes()
break;
}

if ($this->shouldStopValidating($attribute)) {
if ($this->shouldStopValidating($attribute, $rule)) {
break;
}
}
Expand Down Expand Up @@ -909,16 +911,21 @@ protected function validateUsingCustomRule($attribute, $value, $rule)
* Check if we should stop further validations on a given attribute.
*
* @param string $attribute
* @param string|ValidationRule
* @return bool
*/
protected function shouldStopValidating($attribute)
protected function shouldStopValidating($attribute, $rule)
{
$cleanedAttribute = $this->replacePlaceholderInString($attribute);

if ($this->hasRule($attribute, ['Bail'])) {
return $this->messages->has($cleanedAttribute);
}

if ($rule instanceof InvokableValidationRule && $rule->invokable() instanceof Bailable) {
return true;
}

if (isset($this->failedRules[$cleanedAttribute]) &&
array_key_exists('uploaded', $this->failedRules[$cleanedAttribute])) {
return true;
Expand Down
37 changes: 37 additions & 0 deletions tests/Validation/ValidationRuleWithBailableTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace Illuminate\Tests\Validation;

use Closure;
use Illuminate\Contracts\Validation\Bailable;
use Illuminate\Contracts\Validation\ValidationRule;
use Illuminate\Translation\ArrayLoader;
use Illuminate\Translation\Translator;
use Illuminate\Validation\Validator;
use PHPUnit\Framework\TestCase;

class ValidationRuleWithBailableTest extends TestCase
{
public function testFailingStopsFurtherValidation()
{
$trans = new Translator(new ArrayLoader, 'en');
$v = new Validator(
$trans,
['foo' => 'foobar'],
['foo' => [new BailableValidationRule(), 'numeric']],
);
$this->assertFalse($v->passes());
$this->assertEquals(
['foo' => ['Illuminate\Tests\Validation\BailableValidationRule' => []]],
$v->failed()
);
}
}

class BailableValidationRule implements ValidationRule, Bailable
{
public function validate(string $attribute, mixed $value, Closure $fail): void
{
$fail('failed');
}
}
Loading