From 40f8944771db02bd6885e55b54b9399dfd65cdfb Mon Sep 17 00:00:00 2001 From: nshiro <14008307+nshiro@users.noreply.github.com> Date: Fri, 1 Mar 2024 08:46:00 +0900 Subject: [PATCH] [11.x] Fix validated method --- src/Illuminate/Validation/Validator.php | 6 +++- tests/Validation/ValidationValidatorTest.php | 29 ++++++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/src/Illuminate/Validation/Validator.php b/src/Illuminate/Validation/Validator.php index ed5028e322e4..c8ff4c6e912f 100755 --- a/src/Illuminate/Validation/Validator.php +++ b/src/Illuminate/Validation/Validator.php @@ -577,7 +577,11 @@ public function safe(array $keys = null) */ public function validated() { - throw_if($this->invalid(), $this->exception, $this); + if (! $this->messages) { + $this->passes(); + } + + throw_if($this->messages->isNotEmpty(), $this->exception, $this); $results = []; diff --git a/tests/Validation/ValidationValidatorTest.php b/tests/Validation/ValidationValidatorTest.php index a36d204577bd..182ace0ccf74 100755 --- a/tests/Validation/ValidationValidatorTest.php +++ b/tests/Validation/ValidationValidatorTest.php @@ -167,6 +167,35 @@ public function testValidateDoesntThrowOnPass() $this->assertSame(['foo' => 'bar'], $v->validate()); } + public function testValidatedThrowsOnFail() + { + $this->expectException(ValidationException::class); + + $trans = $this->getIlluminateArrayTranslator(); + $v = new Validator($trans, ['foo' => 'bar'], ['baz' => 'required']); + + $v->validated(); + } + + public function testValidatedThrowsOnFailEvenAfterPassesCall() + { + $this->expectException(ValidationException::class); + + $trans = $this->getIlluminateArrayTranslator(); + $v = new Validator($trans, ['foo' => 'bar'], ['baz' => 'required']); + + $v->passes(); + $v->validated(); + } + + public function testValidatedDoesntThrowOnPass() + { + $trans = $this->getIlluminateArrayTranslator(); + $v = new Validator($trans, ['foo' => 'bar'], ['foo' => 'required']); + + $this->assertSame(['foo' => 'bar'], $v->validated()); + } + public function testHasFailedValidationRules() { $trans = $this->getIlluminateArrayTranslator();