1
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 1 year has passed since last update.

laravelの422エラーの解決例

Last updated at Posted at 2023-02-26

何があった?

requestファイルを使ってvalidationを作ったら422エラーが返ってきました。

エラーの原因

requestファイルのvalidationを組んでいるキーを間違ってdbのカラム名にして組んでいたため、存在しないものをチェックしておりエラーを出しました。

TestStoreRequest.php(動かない書き方)
// ==== 前略 ====
    public function rules(): array
    {
        return [
            'name'  => [ // ここの書き方が間違っている
                'required',
                'max:15',
            ],
        ];
    }
// ==== 後略 ====

解決方法

requestファイルのvalidationを組んでいるキーをinputタグのnameに揃えることで解決します。

TestStoreRequest.php(動く書き方)
// ==== 前略 ====
    public function rules(): array
    {
        return [
            'test_name'  => [ // ここを修正
                'required',
                'max:15',
            ],
        ];
    }
// ==== 後略 ====
1
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
1
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?