6
5

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 5 years have passed since last update.

【Laravel5.1】 arrayの中身をバリデーションする

Posted at

FormRequestでのバリデーションについて

arrayで渡ってきたリクエストデータについてどうやってバリデーションを実施するか、ドキュメントみてもわからず悩んでましたが、結果、ループで回してそれぞれ実施しました。

FormRequest
    public function rules()
    {
        $rules = [
            'datas' => 'required|min:1|array',
        ];
        // 配列
        $datas = $this->request->get('datas');
        foreach ($datas as $index => $value) {
            $rules["datas.$index.id"] = 'required|integer|exists:data_tables,id';
            $rules["datas.$index.order"] = 'required|integer|min:0|max:9999999999';
        }
        return $rules;
    }

ただ、こうするとエラーメッセージが index にモロ影響を受けてしまいます。。

The datas.0.id field is required.
The datas.1.id field is required.

indexをうまくエラーメッセージにハンドリングするのを模索中です。

6
5
2

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
6
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?