0

Note : I DO NOT want to redirect to previous url.

My registration form is a modal on the home page where i post my form to auth/register but when validation error happen it redirect me back to home page. Where I want to redirect to another route 'auth/register' where validation error will be shown.

I went through AuthenticatesAndRegistersUsers , ValidatesRequests , UrlGenerator. I found that it use header referer information to redirect me back to previous url.

Is there any way I can set in AuthenticatesAndRegistersUsers traits , postRegister method that if validation error happen from this method then redirect me to this specific url instead of using previous url ?

2 Answers 2

3

Just write your own postRegister() method, and do your own validation and redirection in there. That is how it is designed - so you overload any method that you want to customise.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for suggestion. Not what i expected but solved my problem though. I was expecting laravel to have some magic function to custom set the redirect url hehe
If you are using FormRequest validation - Laravel does have magic function to set the redirect url. But it doesnt in the trait for the Auth section.
1

I solved the problem by modifying AuthenticateAndRegisterUsers.php file located at:

\vendor\laravel\framework\src\Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers.php

I added this line: return redirect('auth/register')->withErrors($validator);

    public function postRegister(Request $request)
{

    $validator = $this->registrar->validator($request->all());

    if ($validator->fails())
    {
        /* $this->throwValidationException(
            $request, $validator
        ); */
        return redirect('auth/register')->withErrors($validator)->withInput();
    }

    $this->auth->login($this->registrar->create($request->all()));

    return redirect($this->redirectPath());
}

3 Comments

It's not standard though, I guess you can overwrite postRegister function somewhere.
I highly suggest you don't do this. These files/codes will get replaced once you update laravel through composer update or install. I did this kind of fixes and so I don't recommend it :)
Right, but still if you overwrite the postRegister method in AuthController, that'd do the trick without any problems, unless they change postRegister method in illuminate.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.