mardi 29 décembre 2015

How to change the redirect method and payload that Laravel's validate return?

I have a problem with validation - if validation fails I get an error.

Route::get('list', 'MainController@list');
Route::post('search', 'MainController@search');
Route::post('create', 'MainController@create');

list shows the full item list and a search form. The form posts to search. search returns a list of items (search results) and the form with two buttons - either search again or create new item. This form posts to create but it calls search method if Search button was pressed. If form was submitted using Create button, the input is validated. This is simplified version of my create method:

public function create(Request $request)
{
    if ($request->has('search'))
        return $this->search($request);

    $this->validate($request, [
        'name' => 'required'
    ]);

    return 0;
}

If search was clicked it all works. If validation passes it all works (and I can include the logic instead of return 0;). But if validation fails I get the following error:

MethodNotAllowedHttpException in RouteCollection.php line 219:
in RouteCollection.php line 219
at RouteCollection->methodNotAllowed(array('POST')) in RouteCollection.php line 206
at RouteCollection->getRouteForMethods(object(Request), array('POST')) in RouteCollection.php line 158
at RouteCollection->match(object(Request)) in Router.php line 802
at Router->findRoute(object(Request)) in Router.php line 670
at Router->dispatchToRoute(object(Request)) in Router.php line 654
at Router->dispatch(object(Request)) in Kernel.php line 246
at Kernel->Illuminate\Foundation\Http\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 139
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in CheckForMaintenanceMode.php line 44
at CheckForMaintenanceMode->handle(object(Request), object(Closure))
at call_user_func_array(array(object(CheckForMaintenanceMode), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 124
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 102
at Pipeline->then(object(Closure)) in Kernel.php line 132
at Kernel->sendRequestThroughRouter(object(Request)) in Kernel.php line 99
at Kernel->handle(object(Request)) in index.php line 53

Problem seems to be tied to routing and indeed browsers debugger shows that it's sending a GET request.

I tried to investigate Laravel's validator implementation but couldn't really understand the flow and find where the request is returned. Is there any chance to affect it in a way so that client gets the result of previous POST again?

If I try to allow GET requests on my search, problem is partially solved. If I search from the full list and then click Create, validation returns me to search again. With no results though so I should still implement some kind of "if validation fails, process this as search request". Well, ok, but I still need what was posted...

If, however, I use the Search again button which posts to create and returns the same view, the validator redirects to GET the create route which gives me more problems to deal with.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire