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

Laravel バリデーション memo*

Last updated at Posted at 2019-04-19

このように、空白や文字数オーバーの時にエラーを表示させる方法。

スクリーンショット (4).png

resauce/views/items/create.blade.php のフォームの上に下記の部分を追加する。

create.blade.php

    @if ($errors->any())
        <div class="alert alert-danger">
            <ul>
                @foreach ($errors->all() as $error)
                    <li>{{ $error }}</li>
                @endforeach
            </ul>
        </div>
    @endif

//以下にフォームのコード>

コントローラーのファイルを以下のように編集

ItemController.php
public function store(Request $request)
    {
        $request->validate([
            'name' => 'required|max:10', //ここでは入力必須、10文字以下と指定
        ]);
        Item::create([
            'name' => $request->input('name'),
        ]);
        return redirect()->route('items.index');
    }

完成!

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?