Skip to content

Commit

Permalink
[11.x] Fix validated method
Browse files Browse the repository at this point in the history
  • Loading branch information
nshiro committed Feb 29, 2024
1 parent 7f84e6e commit 40f8944
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/Illuminate/Validation/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [];

Expand Down
29 changes: 29 additions & 0 deletions tests/Validation/ValidationValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit 40f8944

Please sign in to comment.