You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hi all, just an idea to stop "abusing" FormRequest 😅
Basically after many of years using FormRequest to validate requests I've noticed that we are using it sometimes we use it for validating query parameters in URL or a json parameters. Why? Because it is so much cleaner than $this->validate() in the controller.
By default FormRequest validates all, but if I want to use this $this->json('key') or $this->get('key'), then we are not 100% sure that it is available (because validation joins all data from form / json /etc).
We could provide 2 more "requests" that would validate only its used "data".
I've already made QueryRequest in different project but it would be great to reuse same code as in FormRequest and provide it for all (instead of package - that I could do of-course too).
class AppServiceProvider extends ServiceProvider
{
// Validate query request$this->app->afterResolving(QueryRequest::class, function (QueryRequest$request, Application$application) {
/** @var Factory $validate */$validate = $application->make(Factory::class);
$rules = $application->call([$request, 'rules'], []);
if ($rules === null) {
$rules = [];
}
$query = $request->query();
$validate->make($query, $rules)->validate();
});
// We must pass data from global request$this->app->resolving(QueryRequest::class, function (QueryRequest$request, Application$application) {
QueryRequest::createFrom($application['request'], $request);
});
}
/** * Bootstrap any application services. * * @return void */publicfunctionboot()
{
}
}
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
-
Hi all, just an idea to stop "abusing" FormRequest 😅
Basically after many of years using FormRequest to validate requests I've noticed that we are using it sometimes we use it for validating query parameters in URL or a json parameters. Why? Because it is so much cleaner than
$this->validate()
in the controller.By default FormRequest validates all, but if I want to use this
$this->json('key')
or$this->get('key')
, then we are not 100% sure that it is available (because validation joins all data from form / json /etc).We could provide 2 more "requests" that would validate only its used "data".
I've already made QueryRequest in different project but it would be great to reuse same code as in FormRequest and provide it for all (instead of package - that I could do of-course too).
What do you think?
Best regards, Martin
Beta Was this translation helpful? Give feedback.
All reactions