Lumen - Validation method redirects to root of the website

1k Views Asked by At

my problem is that my form validation fails but the method $this->validate($request, $rules, $messages) wont redirect -> back() instead it redirects to the root page /.

Is there any possibility to change redirect when validation fails?

2

There are 2 best solutions below

0
On

I ran into the same issue yesterday, the solution is to make your own validator instance:

#be sure to include the Validator class
use Validator;

#in your function (if input is coming from a form)
v = Validator::make($request->all(), ['fieldname1', 'fieldname2']);
if ($v->fails()) {
    #return json error
    return response()->json(['success' => false, 'errors' => $v->errors()]);
    #other return could be
    #return view('page/name', 'errors' => $v->errors());
}
// process code here since we've passed validation
0
On

In the trait ValidatesRequests there is a buildFailedValidationResponse function, which redirects when validation fails (And its not an ajax request or json request)

Within the method it calls $this->getRedirectUrl(), so make sure what ever properties this function accesses are set properly