Laravel,  Laravel tips

[Laravel tips] How to redirect users to a specific URL/route when the validation fails

If you are using Form Request to validate your forms before you persist them in the database, when the validation fails, the user is usually redirected back to the same page that initiated the request.

For instance if you are on /new-article page, after you submit the article you are creating, the user will be redirected back automatically to this very same URL if the data she entered failed to pass the validation. This is the desired behavior in majority of the cases.

Lately, I was working on a form where I needed to redirect the user back to another route.

I was working on an application where we have different settings the user could update (from the settings page), and since it wasn’t desirable to send all the data in this page at once, validate it and then update the user settings, I created multiple “small form” each one is in a section of an accordion that updates just a group of related settings. So I could for instance visit /settings to view the settings page with all the sections/cards collapsed, or I could visit (/settings/personal-info) to open the settings page on that particular section.

So I might be visiting /settings/personal-info and then I’d scroll down to another section to update it (privacy for instance). In this case, if the validation fails, the user will be redirected to /settings/personal-info instead of /settings/privacy.

PS: I know this might not be the best way to handle this kind of the situation, but bear with me here, the goal is to explain how we could redirect the user to a different route 🙂

Luckily, we can redirect the user to the desired URL/route by setting up some propriety in the form request class.

Take a look at /vendor/laravel/framework/src/Illuminate/Foundation/Http/FormRequest.php

and you could find these 3 properties:

/**
* The URI to redirect to if validation fails.
*
* @var string
*/
protected $redirect;

/**
* The route to redirect to if validation fails.
*
* @var string
*/
protected $redirectRoute;
  
/**
* The controller action to redirect to if validation fails.
*
* @var string
*/
protected $redirectAction;

So all we need to do is to give the value we want to one (just one) of this properties.

protected $redirectRoute = 'get_all_articles';

PS: if you want to know what would happen if you set up more than one, take a look at the protected function getRedirectUrl() method on the same class.

One issue you might face in this situation is when you want to pass a value to redirectRoute, but you want to pass a route with a parameter like

Route::get('articles/{article}', '[email protected]')→name('view_article');

The solution is to set up the value of this property in the constructor like this:

public function __construct()

{
	$id = ….;
	$this->redirect = route('view_article', $id);
}

Note that I’m using $redirect here instead of the redirectRoute.

I hope you’ll find this tip useful 🙂

%d bloggers like this: