Laravel, form posting using POST - if error, redirect forces GET?

Viewed 52

When posting a form using Laravel, I only want to use "POST" rather than "GET". Most of the validation is done via javascript, but naturally server-side validation is also required. If the server-side validation fails it needs to return the user back with errors, but to do that I use:

return Redirect::back()->withErrors("......

...but redirect happens via GET. Therefore I have to enable both GET and POST in the web route for these pages.

The problem is, the page involves editing a record that already exists, so in order to edit the form an ID is passed in, along with the "@csrf", via POST. Therefore when user has updated the details and submitted, if there is an error it tries to redirect user back to the page but if the page is not available via GET it can't access it.

    //validate data...
     $data = [
            'projectname' => 'required|string|max:255',                
            'email' => 'required|string|email|max:255',
            'password' => 'string|min:6|required',
    ];

    $validator = \Validator::make($request->post(), $data);

    if ($validator->fails()) {
        return \Redirect::back()->withInput()->withErrors($validator);
        //Above redirect doesn't work because the previous page was a "POST"- only page
    }
0 Answers
Related