user4700215
user4700215

Reputation:

how to redirect to a specefic route when validator failed in laravel 5.3?

I am trying to register user from index page but When validator failed then want to redirect to register page.

I am tired to solve this problem . can't customize Illuminate/Foundation/Validation/ValidatesRequests.php page.

Here is the code

protected function getRedirectUrl() {
  return route('register');
}
protected function validator(array $data) {
  $this->getRedirectUrl();
  return Validator::make($data, [ 'name' => 'required|max:255', 'email' => 'required|email|max:255|unique:users', 'password' => 'required|min:6|confirmed', ]);
}

Upvotes: 0

Views: 273

Answers (1)

Sagar Rabadiya
Sagar Rabadiya

Reputation: 4321

add the below method which generate the previous url in your controller and override the default one add following methods in your controller

in your controller where you have defined $this->validate call define below method and use Request

use Illuminate\Http\Request; // add at the top

protected function getRedirectUrl() {
  return route('register');
}

protected function validator(array $data) {
  return $this->validate(request(), [ 'name' => 'required|max:255', 'email' => 'required|email|max:255|unique:users', 'password' => 'required|min:6|confirmed', ]);
}

public function register(Request $request)
{
    $this->validator($request->all());

    event(new Registered($user = $this->create($request->all())));

    $this->guard()->login($user);

    return $this->registered($request, $user)
        ?: redirect($this->redirectPath());
}

Upvotes: 1

Related Questions