Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

aekramer's avatar

Laravel register controller redirect when failed validation

Hello fellow laravel fans,

Currently I have a seperate login and register page (based on laravels make:auth, only tweaked in design and some custom fields) which all work fine, but I recently also added a popup signin box, when a visitor clicks this a window will pop up with a tabbed window allowing them to login/register (without having to redirect to the actual login/register page)

Now my "problem" is that when someone logs in, or tries to register with invalid credentials (email already in use, incorrect password, etc) it will redirect back to the same page, and the errors will be printed but only on the popup box (which will be hidden when the page is loaded) (you can see the errors if you manually open the popup box again)

Is there a way I could tell laravel, if registration validation fails, don't redirect back but redirect to route('register') instead? (The same goes for login)

Thank you kind reader :)

0 likes
3 replies
Sti3bas's avatar

You can override register method in RegisterController by calling the base register method wrapped in try-catch block. Then update redirectTo property of ValidationException when it's catched and re-throw the exception.

public function register(Request $request)
{
    try {
        return $this->baseRegister($request);
    } catch (\Illuminate\Validation\ValidationException $e) {
        $e->redirectTo = route('register');

        throw $e;
    }
}

You also need to make an alias for the register method which is defined in the trait so that you could call it in RegisterController:

use RegistersUsers {
    register as baseRegister;
}
2 likes
Snapey's avatar

I have two modals, login-modal and signup-modal

On the blade file, I include;

<?php 
    if(isset($modal)) {
        echo('$(window).load(function(){$("#' . $modal . '").modal(\'show\');});');
    }          
?>

Then on the auth controller

        $modal = 'login-modal';

I set $modal with the name of the modal I want to appear and pass this to the view.

What I end up with on the html is

    <script>
        $(window).load(function(){$("#login-modal").modal('show');});
    </script>

So whatever name of modal I pass from the controller automatically opens on page load.

See it in action

https://rotarota.net/

1 like
aekramer's avatar

Thanks to both of you! Sti3bas your solution did exactly what I asked for, and I will probably use this as I believe the page is smaller to load than the home page with popup box.

Also thank you Snapey I think that is a really cool solution and I didn't know I could do that, will definetly come in handy in the future!

1 like

Please or to participate in this conversation.