dongshenyu4638
dongshenyu4638
2019-08-05 08:21
浏览 270
已采纳

Laravel:如何重定向到帖子页面

I have a two step validation form, that means the first user input gets validated (within a ValidateTicketData-Controller) and if everything's correct you get to the next form page, which I get to by

Route::post('/{locale?}/ticket/{cat?}', 'TicketController@store')->name('validation');

Problem: On the second page the user is required to upload a file, if he doesn't the validation fails.
If this is the case the validator class immediately redirects which doesn't work since it's a post-route.

So I created a get route like this:

Route::get('/{locale?}/ticket/{cat?}', 'TicketController@store')->name('validation');

and put this in the store-method:

$ticketData = $request->validated();
if ($request->isMethod('get')) {
    $error = 'No pdf-file has been attached.';
    return view ('/validation', compact('ticketData', 'cat', 'error'));
}

I put this into the store-method because this is where the user gets redirected if he won't attach a file on the second page.

But if I now try to send the form without attaching a file I get the message that I've redirected too many times.

I can't find a solution how to redirect to the 'validation`-page with the validated input from the first page (because it gets displayed again) since the Validation class does it automatically.

EDIT

I changed the get-route to this:

Route::get('/{locale?}/ticket/{cat?}', function() {
  $error = 'no pdf tho.';
  return view('/validation', compact('error'));
});

and displayed the $error (if it's not empty) in the view. This worked, but I still don't know how to get the input data from the first page.

I also have this middleware for the $locale

public function handle($request, Closure $next)
{
   $locale = $request->segment(1);
   app()->setLocale($locale);
   return $next($request);
}

which seems to won't let me redirect sometimes, I don't really understand it

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • douxianji3367
    douxianji3367 2019-08-05 09:37
    已采纳

    As I understand it, the problem is that after the first POST, you land on a new page where validation may fail, and if it does, it redirects back to itself (reloads) - which will fail because the POSTed data from the first step is now missing.

    This is a common scenario, and the standard solution is to use PRG - Post/Redirect/Get. This means that after successfully processing a POST, your code Redirects (with a GET request) to a new page, rather than just returning that page's content. This way if the user hits reload on the new page - or if validation on that new page fails - it just reloads (with GET) that new page, without resubmitting the POST.

    In your case, that would look something like (I've used simplified URIs to keep things simple, and I may have mixed up your controller methods):

    // GET the first page where user enters input
    Route::get('/first-page-form', 'TicketController@showFirstPage');
    
    // Handle that data being POSTed
    Route::post('/first-page-processing', 'TicketController@store')->name('validation');
    

    Now your TicketController@store method does its validation, and assuming everything passes, do not just return a view, but instead:

    public function store(...) {
        // Validation code ...
        //
        // Assuming validation OK, save POSTed data to DB, or maybe
        // to session ...
        //
        // All done - redirect to a new page with a GET.
        return redirect('/next-page-form');
    }
    

    You'll need a GET route to handle that:

    // GET the next page where user enters input
    Route::get('/next-page-form', 'TicketController@showNextPage');
    
    // And handle the next data being POSTed
    Route::post('/next-page-processing', 'DatenTicketController@store');
    

    If validation fails, it will simply redirect with GET back to /next-page-form.

    点赞 评论
  • dp518158
    dp518158 2019-08-05 08:30

    You can simply do this with laravel Validation Class By making validation for handling validation response. You can follow below code for this.

    $validator = \Validator::make($request->all(), [
        'validation1' => 'required',
        'validation2' => 'required',
        'validation3' => 'required',
    ]);
    
    if ($validator->fails())
    {
        redirect()->route('your route')->with('error',$validator->errors()->all());
        // or you can return response json for your some ajax or api calls
        return response()->json(['errors'=>$validator->errors()->all()]);
    }
    
    //do whatever on validation passes
    // store data to database
    return response()->json(['success'=>'Record is successfully added']);
    

    You can read it from official documentation about it from here manually-creating-validators

    点赞 评论

相关推荐