Skip to content

Commit a3cf850

Browse files
authored
[11.x] Fix validated method (#50319)
1 parent 0966e0c commit a3cf850

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

src/Illuminate/Validation/Validator.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -577,7 +577,11 @@ public function safe(array $keys = null)
577577
*/
578578
public function validated()
579579
{
580-
throw_if($this->invalid(), $this->exception, $this);
580+
if (! $this->messages) {
581+
$this->passes();
582+
}
583+
584+
throw_if($this->messages->isNotEmpty(), $this->exception, $this);
581585

582586
$results = [];
583587

tests/Validation/ValidationValidatorTest.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,35 @@ public function testValidateDoesntThrowOnPass()
167167
$this->assertSame(['foo' => 'bar'], $v->validate());
168168
}
169169

170+
public function testValidatedThrowsOnFail()
171+
{
172+
$this->expectException(ValidationException::class);
173+
174+
$trans = $this->getIlluminateArrayTranslator();
175+
$v = new Validator($trans, ['foo' => 'bar'], ['baz' => 'required']);
176+
177+
$v->validated();
178+
}
179+
180+
public function testValidatedThrowsOnFailEvenAfterPassesCall()
181+
{
182+
$this->expectException(ValidationException::class);
183+
184+
$trans = $this->getIlluminateArrayTranslator();
185+
$v = new Validator($trans, ['foo' => 'bar'], ['baz' => 'required']);
186+
187+
$v->passes();
188+
$v->validated();
189+
}
190+
191+
public function testValidatedDoesntThrowOnPass()
192+
{
193+
$trans = $this->getIlluminateArrayTranslator();
194+
$v = new Validator($trans, ['foo' => 'bar'], ['foo' => 'required']);
195+
196+
$this->assertSame(['foo' => 'bar'], $v->validated());
197+
}
198+
170199
public function testHasFailedValidationRules()
171200
{
172201
$trans = $this->getIlluminateArrayTranslator();

0 commit comments

Comments
 (0)