I have a website which consist of 2 different login form at 2 places, one on the navbar and the other one is a login page which will be used when the system catches an unlogged visitor.
Can I ask what have I done wrong in my LoginRequest.php where I've set a condition to redirect to a custom login page if there is any sort of error in the login process? I have my codes as 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 suppose to redirect users who login from the nav bar if there is any error to the login page but it doesn't seem to redirect.
Thank you.
If you are using the
validate()
method on theController
then you can overwrite the
buildFailedValidationResponse
from theValidatesRequests
trait present on the baseController
you extend.Something along this line:
if you want to redirect to a specific url, then use
protected $redirect
or if you want to redirect to a named route, then use
$redirectRoute
If you do not want to use the validate method on the request, you may create a validator instance manually using the Validator facade. The make method on the facade generates a new validator instance: Refer to Laravel Validation
Found a solutions. All I need to do is to override the initial response from
like such and it works like a charm.