Skip to content

Commit 0c9b3e9

Browse files
authored
Make rules method in FormRequest optional (laravel#46846)
1 parent 9747b8c commit 0c9b3e9

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

src/Illuminate/Foundation/Http/FormRequest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ protected function getValidatorInstance()
109109
*/
110110
protected function createDefaultValidator(ValidationFactory $factory)
111111
{
112-
$rules = $this->container->call([$this, 'rules']);
112+
$rules = method_exists($this, 'rules') ? $this->container->call([$this, 'rules']) : [];
113113

114114
$validator = $factory->make(
115115
$this->validationData(), $rules,

tests/Foundation/FoundationFormRequestTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,15 @@ public function testValidatedMethodReturnsOnlyRequestedNestedValidatedData()
149149
$this->assertSame('bar', $request->validated('nested.foo'));
150150
}
151151

152+
public function testRequestCanPassWithoutRulesMethod()
153+
{
154+
$request = $this->createRequest([], FoundationTestFormRequestWithoutRulesMethod::class);
155+
156+
$request->validateResolved();
157+
158+
$this->assertEquals([], $request->all());
159+
}
160+
152161
/**
153162
* Catch the given exception thrown from the executor, and return it.
154163
*
@@ -377,3 +386,11 @@ public function authorize()
377386
return Response::allow('baz');
378387
}
379388
}
389+
390+
class FoundationTestFormRequestWithoutRulesMethod extends FormRequest
391+
{
392+
public function authorize()
393+
{
394+
return true;
395+
}
396+
}

0 commit comments

Comments
 (0)