簡體   English   中英

如何使用輸入重定向回表單 - Laravel 5

[英]How to redirect back to form with input - Laravel 5

如果我的表單操作引發異常,如何使用給定的POST參數重定向回我的表單頁面?

您可以使用以下內容:

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());

例如,在您的字段值上編寫舊函數

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

在您的 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->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-方法

希望,它可能適用於所有情況,謝謝。

這肯定會奏效!!!

  $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();
   }

我在 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();
    }

您可以使用這兩個中的任何一個:

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

或者,

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

拉維爾5:

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

只回:

return back()->withInput();

你可以試試這個:

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

<input type="text" class="form-control" name="name" value="{{ old('name') }}">

這個對我有用。

對於 Laravel 5

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

對於 Laravel 6、7 和 8

return back()->withInput();

文檔

  • Laravel 5: https ://laravel.com/docs/5.0/responses#redirects
  • Laravel 6: https ://laravel.com/docs/6.x/responses#redirects
  • Laravel 7: https ://laravel.com/docs/7.x/responses#redirects
  • Laravel 8: https ://laravel.com/docs/8.x/responses#redirects

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM