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バリデータにエラーメッセージを設定【個人的なお勉強アウトプット】

Posted at

バリデータにエラーメッセージを設定

Validator::makeを呼び出す時の引数で指定する。

app/Http/Controllers/HelloController.php
 public function post(Request $request){
        $rules = [
            'name' => 'required',
            'mail' => 'email',
            'age' => 'numeric|between:0,150',
        ];

        $messages = [
                'name.required' => '名前は必ず入力してください。',
                'mail.email' => 'メールアドレスが必要です。',
                'age.numeric' => '年齢を整数で記入ください。',
                'age.between' => '年齢は~150の間で入力ください。'
        ];
        $validator = Validator::make($request->all(),$rules, $messages);
        if($validator->fails()){
            return redirect('/hello')
                ->withErrors($validator)
                ->withInput();
        }
        return view('hello.index',['msg'=>'正しく入力されました!']);
    }
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?