Laravel 5 Custom Valid Redirection

I have a website that consists of 2 different login forms in 2 places, one on the navigation bar and the other is the login page that will be used when the system catches an unregistered visitor.

May I ask what I did wrong in my LoginRequest.php, where I set the condition for redirecting to the user login page, if there is any error during the registration process? I have my codes as shown below:

<?php namespace App\Http\Requests; use App\Http\Requests\Request; class LoginRequest extends Request { /** * Determine if the user is authorized to make this request. * * @return bool */ public function authorize() { return true; } /** * Get the validation rules that apply to the request. * * @return array */ public function rules() { return [ 'login_email' => 'required', 'login_password' => 'required' ]; } public function messages() { return [ 'login_email.required' => 'Email cannot be blank', 'login_password.required' => 'Password cannot be blank' ]; } public function redirect() { return redirect()->route('login'); } } 

The code is supposed to redirect users who enter from the navigation panel if there is any error on the login page, but it is not redirected.

Thanks.

+7
php laravel laravel-5 laravel-routing laravel-validation
source share
4 answers

Found solutions. All I have to do is cancel the initial answer from

FormRequest.php

as such, and it works like a charm.

 public function response(array $errors) { // Optionally, send a custom response on authorize failure // (default is to just redirect to initial page with errors) // // Can return a response, a view, a redirect, or whatever else if ($this->ajax() || $this->wantsJson()) { return new JsonResponse($errors, 422); } return $this->redirector->to('login') ->withInput($this->except($this->dontFlash)) ->withErrors($errors, $this->errorBag); } 
+6
source share

if you want to redirect to a specific url use protected $redirect

 class LoginRequest extends Request { protected $redirect = "/login#form1"; // ... } 

or if you want to redirect to a named route, use $redirectRoute

 class LoginRequest extends Request { protected $redirectRoute = "session.login"; // ... } 
+1
source share

If you use the validate() method on Controller

 $this->validate($request, $rules); 

then you can overwrite buildFailedValidationResponse from the ValidatesRequests attribute present on the Controller base that you are expanding.

Something along this line:

 protected function buildFailedValidationResponse(Request $request, array $errors) { if ($request->expectsJson()) { return new JsonResponse($errors, 422); } return redirect()->route('login'); } 
+1
source share

If you do not want to use the validation method in the request, you can manually create the validator instance using the Validator facade. The make method on the facade generates a new validation instance: see Validation Validation

  public function store(Request $request) { $validator = Validator::make($request->all(), [ 'title' => 'required|unique:posts|max:255', 'body' => 'required', ]); if ($validator->fails()) { return redirect('post/create') ->withErrors($validator) ->withInput(); } // Store the blog post... } 
0
source share

All Articles