Skip to content

Commit

Permalink
Make rules method in FormRequest optional (laravel#46846)
Browse files Browse the repository at this point in the history
  • Loading branch information
driesvints authored Apr 21, 2023
1 parent 9747b8c commit 0c9b3e9
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Illuminate/Foundation/Http/FormRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ protected function getValidatorInstance()
*/
protected function createDefaultValidator(ValidationFactory $factory)
{
$rules = $this->container->call([$this, 'rules']);
$rules = method_exists($this, 'rules') ? $this->container->call([$this, 'rules']) : [];

$validator = $factory->make(
$this->validationData(), $rules,
Expand Down
17 changes: 17 additions & 0 deletions tests/Foundation/FoundationFormRequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,15 @@ public function testValidatedMethodReturnsOnlyRequestedNestedValidatedData()
$this->assertSame('bar', $request->validated('nested.foo'));
}

public function testRequestCanPassWithoutRulesMethod()
{
$request = $this->createRequest([], FoundationTestFormRequestWithoutRulesMethod::class);

$request->validateResolved();

$this->assertEquals([], $request->all());
}

/**
* Catch the given exception thrown from the executor, and return it.
*
Expand Down Expand Up @@ -377,3 +386,11 @@ public function authorize()
return Response::allow('baz');
}
}

class FoundationTestFormRequestWithoutRulesMethod extends FormRequest
{
public function authorize()
{
return true;
}
}

0 comments on commit 0c9b3e9

Please sign in to comment.