【问题标题】:How to redirect back to form with input - Laravel 5如何使用输入重定向回表单 - Laravel 5
【发布时间】:2015-09-13 21:46:32
【问题描述】:

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

【问题讨论】:

  • 你知道我必须赞成你的问题只是因为你同时提出和回答。
  • 认为这对其他人会有所帮助。找不到解决方案,所以我挖掘了源代码,看看它是如何通过表单验证自动处理的
  • 我一直很感激。

标签: php forms laravel redirect laravel-5


【解决方案1】:

您可以使用以下内容:

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

如果你使用的是Form Request Validation,这正是 Laravel 将错误和给定输入重定向回来的方式。

摘自\Illuminate\Foundation\Validation\ValidatesRequests

return redirect()->to($this->getRedirectUrl())
                    ->withInput($request->input())
                    ->withErrors($errors, $this->errorBag());

【讨论】:

  • 不,它不维护您的输入参数
  • 当然,我提到了这个:return redirect()->back()->withInput(); 这只是一个方便的宏。
  • 你应该知道你需要在<input type="text" name="username" value="{{ old('username') }}"> 的表单中使用这个,正如@Vishal_Rambhiya 回复的那样
【解决方案2】:

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

<input type="text" name="username" value="{{ old('username') }}">

【讨论】:

  • 谢谢!我正在使用 Laravel 5.2,这也适用于我。除了刀片模板中的old() 函数外,我们还需要在控制器中使用withInput() 使其工作。喜欢 => if($validate-&gt;fails()) return redirect("somepage")-&gt;withErrors($validate)-&gt;withInput();
  • 关于如何为 select 执行此操作的任何想法
  • @NiteshVerma 你可以像{{if(isset(old('select')){'$("select option[value=\''.old('select').'\']").attr("selected",true)'}};一样使用jquery
  • Laravel 4.2 有类似的解决方案吗?
  • 在选择框上怎么做?
【解决方案3】:

在您的 HTML 中,您必须使用 value = {{ old('') }}。如果不使用它,您将无法取回值,因为会话将存储在其缓存中。

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

<input type="text" name="name" value="{{ old('name') }}" />

现在,如果重定向出错,您可以在提交后获取值。

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

正如@infomaniac所说,您也可以直接使用Input class

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

添加: 如果只显示特定字段,则使用$request-&gt;only()

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

更新: 在此处获取更多关于 Laravel 表单输入的示例和真实演示 - https://devsenv.com/tutorials/how-to-redirect-back-in-laravel-with-form-input-and-many-possible-ways

希望,它可能适用于所有情况,谢谢。

【讨论】:

    【解决方案4】:

    这肯定会起作用 !!!

      $v = Validator::make($request->all(),[
      'name' => ['Required','alpha']
      ]);
    
       if($v->passes()){
         print_r($request->name);
       }
       else{
         //this will return the errors & to check put "dd($errors);" in your blade(view)
         return back()->withErrors($v)->withInput();
       }
    

    【讨论】:

      【解决方案5】:

      您可以使用以下任何一种:

      return redirect()-&gt;back()-&gt;withInput(Input::all())-&gt;with('message', 'Some message');

      或者,

      return redirect('url_goes_here')-&gt;withInput(Input::all())-&gt;with('message', 'Some message');

      【讨论】:

      • 您的回答并未对之前的回答添加任何新内容。它还增加了混乱,因为您没有解释“url”是什么。你也已经回答了这个问题。请删除您的一个答案。您可以编辑自己以前的帖子。
      【解决方案6】:

      我在 Laravel 5.3 中像这样处理验证异常。如果你使用 Laravel Collective,它会在输入旁边自动显示错误,如果你使用 laracasts/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();
          }
      

      【讨论】:

        【解决方案7】:

        Laravel 5:

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

        仅用于背面:

        return back()->withInput();
        

        【讨论】:

        • return back()-&gt;withInput();
        • 不错!我将 $request-&gt;all() 传递给此方法,但很高兴知道这不是必需的。
        【解决方案8】:

        你可以试试这个:

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

        【讨论】:

          【解决方案9】:

          对于 Laravel 5

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

          对于 Laravel 6、7 和 8

          return back()->withInput();
          

          文档

          【讨论】:

            【解决方案10】:
            $request->flash('request',$request);
            
            <input type="text" class="form-control" name="name" value="{{ old('name') }}">
            

            它对我有用。

            【讨论】:

              猜你喜欢
              • 2019-12-23
              • 2015-12-18
              • 2018-07-01
              • 1970-01-01
              • 2015-11-10
              • 2015-07-12
              • 2018-01-05
              • 1970-01-01
              • 2015-05-22
              相关资源
              最近更新 更多