dongxingji3882 2016-11-04 17:47
浏览 78
已采纳

重新提交表单时出现TokenMismatchException

I am using Laravel 5.3 and the laravelcollective/html form helpers.

When I submit a form, if the validation fails it takes you back to the same form using return redirect()->back()->with

Then if I resubmit the same form again I will get a TokenMismatchException, what happenned is that the csrf token did not refreshed after the form was reloaded.

Any ideas of how can I get the token refreshed?

Update 11/07

To open the form I use the following:

{!! Form::open(['url'=>'/user/create', 'method'=>'post', 'id'=>'create']) !!}

Which automatically adds the token field.

But Also I have tried adding the token manually, like this:

{!! Form::open(['url'=>'/user/create', 'method'=>'post', 'id'=>'create']) !!}
{!! Form::token() !!}

Here the token is actually created twice, both tokens are be the same.

  • 写回答

1条回答 默认 最新

  • dongshaoxiong0012 2016-11-07 18:24
    关注

    The problem was that I was sending the value _token back to the view. What I was doing was:

    return redirect()
    ->back()
    ->with(
    ['errors' => $validator->errors()->all()] 
    + $request->input()
    );
    

    Where the $request->input('_token') was not being filtered. Instead I have changed it for the following which works, as it does filter _token:

    return redirect()
    ->back()
    ->withErrors($validator->errors()->all())
    ->withInput($request->input());
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 java在应用程序里获取不到扬声器设备
  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
  • ¥60 许可证msc licensing软件报错显示已有相同版本软件,但是下一步显示无法读取日志目录。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系