0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Laravelのvalidate時の自動リダイレクトを自前で行う

Last updated at Posted at 2020-03-18

コントローラーで以下のようバリデーションを行った際、バリデーションエラーがあれば自動的にリダイレクトレスポンスが生成され、入力値とフラッシュメッセージがリダイレクト先に渡されます。

$validatedData = $request->validate([
    'title' => 'required|unique:posts|max:255',
    'body' => 'required',
]);

このリダイレクトをバリデーションを使わずに行いたい場合、withInput()で入力値を渡し、withErrors()でエラーのフラッシュメッセージを渡すことが可能。

$errors = [
    'error_key' => 'エラーメッセージ',
];

return redirect()->back()->withInput()->withErrors($errors);
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?