php怎么重定位,PHP的-如何重定向回输入形式-Laravel 5

这篇博客讨论了在Laravel5中如何处理表单验证异常并重定向回表单页面。当表单操作触发异常时,可以使用`returnRedirect::back()->withInput(Input::all())`来保留POST参数并返回到表单,同时展示错误信息。此外,还提到了使用`withErrors`方法传递验证错误,并展示了如何在视图中显示这些错误。
摘要由CSDN通过智能技术生成

PHP的-如何重定向回输入形式-Laravel 5

如果我的表单操作引发异常,如何使用给定的POST参数重定向回到我的表单页面?

8个解决方案

67 votes

您可以使用以下内容:

return Redirect::back()->withInput(Input::all());

如果您使用的是表单请求验证,这正是Laravel将错误和给定输入重定向回您的方式。

摘自return redirect()->to($this->getRedirectUrl())

->withInput($request->input())

->withErrors($errors, $this->errorBag());:

return redirect()->to($this->getRedirectUrl())

->withInput($request->input())

->withErrors($errors, $this->errorBag());

infomaniac answered 2020-02-18T06:25:09Z

39 votes

在您的字段值上写旧函数例如

Vishal Rambhiya answered 2020-02-18T06:25:28Z

14 votes

在您的HTML中,您必须使用$request->only()。如果不使用它,您将无法获取该值,因为会话将存储在其缓存中。

就像进行名称验证一样,这将是-

现在,如果重定向发生错误,则可以在提交后获取值。

return redirect()->back()->withInput();

正如@infomaniac所说,您也可以直接使用$request->only(),

return Redirect::back()->withInput(Input::all());

加:如果仅显示特定字段,请使用$request->only()

return redirect()->back()->withInput($request->only('name'));

希望,它可能在所有情况下都有效,谢谢。

Maniruzzaman Akash answered 2020-02-18T06:26:11Z

4 votes

我这样处理Laravel 5.3中的验证异常。 如果您使用Laravel Collective,它将在输入旁边自动显示错误;如果您使用laracast / flash,它还将显示第一个验证错误作为通知。

Handler.php呈现:

public function render($request, Exception $e)

{

if ($e instanceof \Illuminate\Validation\ValidationException) {

return $this->handleValidationException($request, $e);

}

(..)

}

和功能:

protected function handleValidationException($request, $e)

{

$errors = @$e->validator->errors()->toArray();

$message = null;

if (count($errors)) {

$firstKey = array_keys($errors)[0];

$message = @$e->validator->errors()->get($firstKey)[0];

if (strlen($message) == 0) {

$message = "An error has occurred when trying to register";

}

}

if ($message == null) {

$message = "An unknown error has occured";

}

\Flash::error($message);

return \Illuminate\Support\Facades\Redirect::back()->withErrors($e->validator)->withInput();

}

Rav answered 2020-02-18T06:26:40Z

3 votes

Laravel 5:

return redirect(...)->withInput();

仅用于背部:

return back()->withInput();

Luca C. answered 2020-02-18T06:27:05Z

0 votes

这肯定会工作!

$v = Validator::make($request->all(),[

'name' => ['Required','alpha']

]);

if($v->passes()){

$ab = $request->name;

print_r($ab);

}

else{

//this will return the errors & to check put "dd($errors);" in your blade(view)

return back()->withErrors($v)->withInput();

}

Aditya Tomar answered 2020-02-18T06:27:25Z

0 votes

$request->flash('request',$request);

这个对我有用。

Tuấn Anh Trần Văn answered 2020-02-18T06:27:45Z

0 votes

您可以尝试以下方法:

return redirect()->back()->withInput(Input::all())->with('message', 'Something

went wrong!');

RashedRahat answered 2020-02-18T06:28:05Z

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值