JohnRivs's avatar

Change redirection URL after ValidationException in Lumen

According to Lumen's docs:

However, if validation fails, an Illuminate\Contracts\Validation\ValidationException will be thrown. This exception is automatically caught and a redirect is generated to the user's previous location.

In my case, instead of redirecting back to where the form was fired, it sends me back to the root.

How do I make sure it redirects back? How can I change the URL if I ever need to?

Trying to avoid this (from the docs too):

$v = Validator::make($request->all(), [
    'title' => 'required|unique|max:255',
    'body' => 'required',
]);

if ($v->fails()) {
    return redirect()->back()->withErrors($v->errors());
}
0 likes
13 replies
bobbybouwmann's avatar

It always calls return redirect()->back() for you.. I don't understand your question I guess

bestmomo's avatar

Override getRedirectUrl in your controller:

protected function getRedirectUrl()
{
    return your_url_there;
}
JohnRivs's avatar

@bestmomo I found that method in the ValidatesRequests trait. Noticed it uses app('session'), so I thought uncommenting the StartSession middleware in bootstrap\app.php would actually redirect me back. No luck :/

Can't make it redirect the user back with input.

JohnRivs's avatar

@bobbybouwmann also, it doesn't return redirect()->back(). The trait responsible for throwing the ValidationException has this method:

protected function buildFailedValidationResponse(Request $request, array $errors)
{
    if ($request->ajax() || $request->wantsJson()) {
        return new JsonResponse($errors, 422);
    }

    return redirect()->to($this->getRedirectUrl())
                    ->withInput($request->input())
                    ->withErrors($errors, $this->errorBag());
}
JohnRivs's avatar

@bestmomo if by 'set the session' you mean what I responded to you earlier about uncommenting the middleware in bootstrap\app.php, then yes. Do I need to do something extra?

bestmomo's avatar

I cant reproduce your issue with Lumen, all is working fine for me.

JohnRivs's avatar

Anything wrong with my form maybe?

<form action="{ url('whatever') }" method="POST">
    <input type="text" name="name">
    <input type="submit" value="send">
</form>

Using single brackets in this post so the block code can actually display the text inside it.

JohnRivs's avatar

I just installed a fresh copy of Lumen using the installer. I have this in my .env file.

CACHE_DRIVER=file
SESSION_DRIVER=file
QUEUE_DRIVER=database

Routes.php

$app->get('/', function() {
    return view('welcome');
});

$app->post('whatever', function() {
    return redirect()->back()->withInput();
});

welcome.php

<form action="{ url('whatever') }" method="POST">
    <input type="text" name="name">
    <input type="submit" value="send">
</form>

It redirects back, but doesn't fill in the input. Tested this on a Laravel app and it works.

JohnRivs's avatar

I have no idea what's going on. I'm sure I'm overlooking something stupid.. If I use illuminate/html or laravelcollective/html to build my forms, redirects back with input. However, when I copy the same exact output of that form and write it manually, it doesn't fill in the input when it redirects back.

What the hell.

bobbybouwmann's avatar

You need to add the data to the input field if you want that

<input type="text" name="name" value="{{ old('name') }}>

Please or to participate in this conversation.