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

[9.x] Make rules method in FormRequest optional #46846

Merged
merged 1 commit into from
Apr 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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;
}
}