Help us understand the problem. What is going on with this article?

Laravel バリデーションチェック構文

More than 1 year has passed since last update.

バリデーション

基本構文

this->validate($request,[検証設定の配列]);

エラーがあるかどうかチェック

$errors->has(項目名)

エラーメッセージを表示

$errors->first(項目名)

使用例

form nameにname,mail,ageが用意されており、それぞれバリデーションチェックをするコード

下記の機能も追加
①バリデートエラー文を吐き出す
②記述したものが消えないように処理

view

/App/Http/Controllers/hello.controller.php
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Http\Response;


class HelloController extends Controller
{
    public function index(Request $request)
    {
        return view('hello.index',['msg'=>'フォームを入力:']);
    }

    public function post(Request $request) {
        $validate_rule = [
            'name' => 'required',
            'mail' => 'email',
            'age' => 'numeric|between:0,150',
        ];
        $this->validate($request,$validate_rule);
        return view('hello.index',['msg'=>'正しく入力されました!']);
    }
}

バリデートエラー文をまとめて表示させたい場合

/resource/views/hello/index.blade.php
@section('content')
    <p>{{$msg}}</p>
    <div>
        <ul>
            @foreach($errors->all() as $error)
                <li>{{$error}}</li>
            @endforeach
        </ul>
    </div>
    <table>
        <form action="" method="post">
        {{csrf_field()}}
            <tr>
                <th>name:</th>
                <td><input type="text" name="name" value="{{old('name')}}"></td>
            </tr>
            <tr>
                <th>mail:</th>
                <td><input type="text" name="mail" value="{{old('mail')}}"></td>
            </tr>
            <tr>
                <th>age:</th>
                <td><input type="text" name="age" value=""{{old('age')}}></td>
            </tr>
            <tr>
                <th></th>
                <td><input type="submit" value="send"></td>
            </tr>
        </form>
    </table>
@endsection

バリデートエラーの箇所の上に表示させたい場合

/resource/views/hello/index.blade.php
@section('content')
    <p>{{$msg}}</p>

    <table>
        <form action="" method="post">
        {{csrf_field()}}
            @if($errors->has('name'))
                <tr>
                    <th>ERROR:</th>
                    <td>{{$errors->first('name')}}</td>
                </tr>
            @endif
            <tr>
                <th>name:</th>
                <td><input type="text" name="name" value="{{old('name')}}"></td>
            </tr>
            @if($errors->has('mail'))
                <tr>
                    <th>ERROR:</th>
                    <td>{{$errors->first('mail')}}</td>
                </tr>
            @endif
            <tr>
                <th>mail:</th>
                <td><input type="text" name="mail" value="{{old('mail')}}"></td>
            </tr>
            @if($errors->has('age'))
                <tr>
                    <th>ERROR:</th>
                    <td>{{$errors->first('age')}}</td>
                </tr>
            @endif
            <tr>
                <th>age:</th>
                <td><input type="text" name="age" value=""{{old('age')}}></td>
            </tr>
            <tr>
                <th></th>
                <td><input type="submit" value="send"></td>
            </tr>
        </form>
    </table>
@endsection
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした