From 8a04fca24b5b947acf45706c60d038052b8c1540 Mon Sep 17 00:00:00 2001 From: Ben Holmen Date: Mon, 29 Mar 2021 15:57:30 -0500 Subject: [PATCH 1/2] Add key option to $request->validated() Co-authored-by: mattstauffer --- .../Foundation/Http/FormRequest.php | 12 +++++++++-- .../Foundation/FoundationFormRequestTest.php | 20 +++++++++++++++++++ 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/src/Illuminate/Foundation/Http/FormRequest.php b/src/Illuminate/Foundation/Http/FormRequest.php index 8c2da9699600..e82bf967bb2b 100644 --- a/src/Illuminate/Foundation/Http/FormRequest.php +++ b/src/Illuminate/Foundation/Http/FormRequest.php @@ -188,10 +188,18 @@ protected function failedAuthorization() /** * Get the validated data from the request. * - * @return array + * @param string|null $key + * @param string|array|null $default + * @return mixed */ - public function validated() + public function validated($key = null, $default = null) { + if (! is_null($key)) { + return data_get( + $this->validator->validated(), $key, $default + ); + } + return $this->validator->validated(); } diff --git a/tests/Foundation/FoundationFormRequestTest.php b/tests/Foundation/FoundationFormRequestTest.php index d394566ce6cb..8c5d4476644a 100644 --- a/tests/Foundation/FoundationFormRequestTest.php +++ b/tests/Foundation/FoundationFormRequestTest.php @@ -115,6 +115,26 @@ public function test_after_validation_runs_after_validation() $this->assertEquals(['name' => 'Adam'], $request->all()); } + public function testValidatedMethodReturnsOnlyRequestedValidatedData() + { + $request = $this->createRequest(['name' => 'specified', 'with' => 'extras']); + + $request->validateResolved(); + + $this->assertEquals('specified', $request->validated('name')); + } + + public function testValidatedMethodReturnsOnlyRequestedNestedValidatedData() + { + $payload = ['nested' => ['foo' => 'bar', 'baz' => ''], 'array' => [1, 2]]; + + $request = $this->createRequest($payload, FoundationTestFormRequestNestedStub::class); + + $request->validateResolved(); + + $this->assertEquals('bar', $request->validated('nested.foo')); + } + /** * Catch the given exception thrown from the executor, and return it. * From 4645617e50d34313274b2fd5cc20764db0d7ba01 Mon Sep 17 00:00:00 2001 From: Ben Holmen Date: Mon, 29 Mar 2021 15:59:27 -0500 Subject: [PATCH 2/2] Make FoundationFormRequestTest method names consistent --- tests/Foundation/FoundationFormRequestTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Foundation/FoundationFormRequestTest.php b/tests/Foundation/FoundationFormRequestTest.php index 8c5d4476644a..1d68e487235e 100644 --- a/tests/Foundation/FoundationFormRequestTest.php +++ b/tests/Foundation/FoundationFormRequestTest.php @@ -106,7 +106,7 @@ public function testPrepareForValidationRunsBeforeValidation() $this->createRequest([], FoundationTestFormRequestHooks::class)->validateResolved(); } - public function test_after_validation_runs_after_validation() + public function testAfterValidationRunsAfterValidation() { $request = $this->createRequest([], FoundationTestFormRequestHooks::class);