By default, if there are any validation errors, Laravel redirects the users back to the same page, displaying it from the very top.
In some cases, that’s fine.
But, for my commenting system, I wanted the validation errors to be displayed above the commenting form, and the users to be redirected there after the refresh, not at the beginning of the page.

I googled for a solution, but most of them were meh or didn’t work.
The most offered solution is to manually create validators in your Controller and add the redirect there.
Something like this:
public function store(Request $request)
{
$validator = Validator::make($request->all(), [
'title' => 'required|unique:posts|max:255',
'body' => 'required',
]);
if ($validator->fails()) {
return redirect('post/create')
->withErrors($validator)
->withInput();
}
// Store the blog post...
}
That’s not the best practice. It will unnecessarily cluster your controllers.
The second solution I’ve found is to use one of these custom redirects in your Request:
protected $redirect;
protected $redirectRoute;
protected $redirectAction;
I tried protected $redirect = '#comment-form';
, but it was being added at the end of the route URI.
I have this in routes:
Route::resource('/store/comments', 'PostCommentController');
So, the #comment-form
anchor from the $redirect
property was being added here:
/store/comments#comment-form
And it was displaying a blank page because that route URI is only used for processing the form.
But, luckily, I found a solution that lets me use the Form Request method and also does the job that I want.
Change the Validation Redirect Path for Form Requests
The solution is really simple.
All you have to do is to add a line of code in your custom Request, in the rules()
method.
In my case, it was this one:
$this->redirect = url()->previous() . '#comment-form';

I still wanted Laravel to redirect to the same page if there are validation errors, but to a specific section in that page using an anchor.
So, what you have to do is to add the redirection path that you want for $this->redirect
.
Where does $this->redirect
come from?
$this->redirect
is the protected $redirect;
property from the FormRequest
class which extends all of your other custom Requests classes.

It can be found in:
\vendor\laravel\framework\src\Illuminate\Foundation\Http\FormRequest.php
If you didn’t create the Request using artisan
Make sure you add this at the top:
use Illuminate\Foundation\Http\FormRequest;
And also extend your class to FormRequest
:
class YourCustomRequest extends FormRequest

If you know a better solution, let me and others know in the comment section!
That’s a Wrap
If you have questions or thoughts, please leave a comment or send me a message using the contact page.
Don’t forget to share the post to help out others!
Hi!
When you use validation for many pages you can’t use the same id..
Have other solution?
Hello!
Not that I can think of now, unfortunately. I’ll have to check, but haven’t used Laravel in a while…
What are you using now?
Nothing. I haven’t been using Laravel because I haven’t been developing anything lately, unfortunately. I didn’t have any idea for a project, and I couldn’t lose time because I needed money, so I had to focus on that…