cjmling
cjmling

Reputation: 7278

Laravel : redirect to other url not previous on failed form validation

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 ?

Upvotes: 0

Views: 2102

Answers (2)

Ali Padida
Ali Padida

Reputation: 1939

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());
}

Upvotes: 1

Laurence
Laurence

Reputation: 60038

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.

Upvotes: 3

Related Questions