I am using Laravel's form request validation, which I find very useful, and is making my controllers a lot tidier.
My more complex validation pieces, which, if the Validator
fails, should return back to the previous page, with the user's old input.
I also, sometimes, have to specify a named error bag, which again, doesn't seem to come built in as an option to the request forms.
Unfortunately, this additional error functions doesn't appear to come by default with the form requests validator.
At the moment I have:
$validator = Validator::make($request->all(), [
'id' => 'required|numeric',
'name' => 'required|alpha_spaces',
'email' => 'required|email',
]);
if ($validator->fails()) {
return back()
->withErrors($validator, 'aNamedErrorBag')
->withInput();
}
Is there a way to extract the rules into a request validator file, and return with input to the named error bag aNamedErrorBag
, should the validator fail?
Many thanks