@@ -164,59 +164,115 @@ public function testValidatedReturnsOnlyFieldsCoveredByRules(): void
164164 }
165165
166166 // -------------------------------------------------------------------------
167- // __get / __isset - property-style access to validated fields
167+ // Explicit access to validated fields
168168 // -------------------------------------------------------------------------
169169
170- public function testMagicGetReturnsValidatedFieldValue (): void
170+ public function testGetValidatedReturnsValidatedFieldValue (): void
171171 {
172172 service ('superglobals ' )->setPost ('title ' , 'Hello World ' );
173173 service ('superglobals ' )->setPost ('body ' , 'Some body text ' );
174174
175175 $ formRequest = new ValidPostFormRequest ($ this ->makeRequest ());
176176 $ formRequest ->resolveRequest ();
177177
178- $ this ->assertSame ('Hello World ' , $ formRequest ->title );
179- $ this ->assertSame ('Some body text ' , $ formRequest ->body );
178+ $ this ->assertSame ('Hello World ' , $ formRequest ->getValidated ( ' title ' ) );
179+ $ this ->assertSame ('Some body text ' , $ formRequest ->getValidated ( ' body ' ) );
180180 }
181181
182- public function testMagicGetReturnsNullForMissingField (): void
182+ public function testGetValidatedReturnsNullForMissingField (): void
183183 {
184184 service ('superglobals ' )->setPost ('title ' , 'Hello World ' );
185185 service ('superglobals ' )->setPost ('body ' , 'Some body text ' );
186186
187187 $ formRequest = new ValidPostFormRequest ($ this ->makeRequest ());
188188 $ formRequest ->resolveRequest ();
189189
190- $ this ->assertNull ($ formRequest ->nonexistent ); // @phpstan-ignore property.notFound
190+ $ this ->assertNull ($ formRequest ->getValidated ( ' nonexistent ' ));
191191 }
192192
193- public function testMagicGetReturnsNullBeforeValidationRuns (): void
193+ public function testGetValidatedReturnsDefaultForMissingField (): void
194+ {
195+ service ('superglobals ' )->setPost ('title ' , 'Hello World ' );
196+ service ('superglobals ' )->setPost ('body ' , 'Some body text ' );
197+
198+ $ formRequest = new ValidPostFormRequest ($ this ->makeRequest ());
199+ $ formRequest ->resolveRequest ();
200+
201+ $ this ->assertSame ('fallback ' , $ formRequest ->getValidated ('nonexistent ' , 'fallback ' ));
202+ }
203+
204+ public function testGetValidatedReturnsNullBeforeValidationRuns (): void
194205 {
195206 $ formRequest = new ValidPostFormRequest ($ this ->makeRequest ());
196207
197- $ this ->assertNull ($ formRequest ->title );
208+ $ this ->assertNull ($ formRequest ->getValidated ( ' title ' ) );
198209 }
199210
200- public function testMagicIssetReturnsTrueForValidatedField (): void
211+ public function testHasValidatedReturnsTrueForValidatedField (): void
201212 {
202213 service ('superglobals ' )->setPost ('title ' , 'Hello World ' );
203214 service ('superglobals ' )->setPost ('body ' , 'Some body text ' );
204215
205216 $ formRequest = new ValidPostFormRequest ($ this ->makeRequest ());
206217 $ formRequest ->resolveRequest ();
207218
208- $ this ->assertTrue (isset ( $ formRequest ->title ));
219+ $ this ->assertTrue ($ formRequest ->hasValidated ( ' title ' ));
209220 }
210221
211- public function testMagicIssetReturnsFalseForMissingField (): void
222+ public function testHasValidatedReturnsFalseForMissingField (): void
212223 {
213224 service ('superglobals ' )->setPost ('title ' , 'Hello World ' );
214225 service ('superglobals ' )->setPost ('body ' , 'Some body text ' );
215226
216227 $ formRequest = new ValidPostFormRequest ($ this ->makeRequest ());
217228 $ formRequest ->resolveRequest ();
218229
219- $ this ->assertFalse (isset ($ formRequest ->nonexistent )); // @phpstan-ignore property.notFound
230+ $ this ->assertFalse ($ formRequest ->hasValidated ('nonexistent ' ));
231+ }
232+
233+ public function testGetValidatedAndHasValidatedSupportDotSyntax (): void
234+ {
235+ service ('superglobals ' )->setPost ('post ' , [
236+ 'title ' => 'Hello World ' ,
237+ 'meta ' => [
238+ 'slug ' => 'hello-world ' ,
239+ ],
240+ ]);
241+
242+ $ formRequest = new class ($ this ->makeRequest ()) extends FormRequest {
243+ public function rules (): array
244+ {
245+ return [
246+ 'post.title ' => 'required ' ,
247+ 'post.meta.slug ' => 'required ' ,
248+ ];
249+ }
250+ };
251+
252+ $ formRequest ->resolveRequest ();
253+
254+ $ this ->assertSame ('Hello World ' , $ formRequest ->getValidated ('post.title ' ));
255+ $ this ->assertSame ('hello-world ' , $ formRequest ->getValidated ('post.meta.slug ' ));
256+ $ this ->assertTrue ($ formRequest ->hasValidated ('post.meta.slug ' ));
257+ }
258+
259+ public function testHasValidatedReturnsTrueForNullValidatedField (): void
260+ {
261+ service ('superglobals ' )->setServer ('CONTENT_TYPE ' , 'application/json ' );
262+
263+ $ formRequest = new class ($ this ->makeRequest ('{"note":null} ' )) extends FormRequest {
264+ public function rules (): array
265+ {
266+ return ['note ' => 'permit_empty ' ];
267+ }
268+ };
269+
270+ $ formRequest ->resolveRequest ();
271+
272+ $ this ->assertSame (['note ' => null ], $ formRequest ->validated ());
273+ $ this ->assertNull ($ formRequest ->getValidated ('note ' ));
274+ $ this ->assertNull ($ formRequest ->getValidated ('note ' , 'fallback ' ));
275+ $ this ->assertTrue ($ formRequest ->hasValidated ('note ' ));
220276 }
221277
222278 // -------------------------------------------------------------------------
0 commit comments