LoginSignup
0
0

More than 1 year has passed since last update.

laravel バリデーション

Last updated at Posted at 2021-06-10

image.png

ルーティング

/routes/web.php

<?php
Route::get('/validation_sample', 'SampleController@validationSampleForm');

Route::post('/validation_sample', 'SampleController@validationSample');

コントローラー

app/Http/Controllers/SampleController.php

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;

class SampleController extends Controller{


      public function validationSampleForm(){
        return view('samples.validation_sample_form', [
          'title' => 'バリデーション用サンプルフォーム',
        ]);
      }



      public function validationSample(Request $request){

        $request->validate([
          // ここに項目ごとのバリデーションルールを記述
          'name' => ['required', 'min:2', 'max:20'],
          'user_name' => ['required', 'min:3', 'max:20'],
          'email' => ['required', 'email'],
          'age' => ['required', 'integer', 'min:16', 'max:150'],
          'birthdate' => ['required', 'date', 'before:now', 'after_or_equal:1900-1-1'],
        ]);

        return redirect('/validation_sample');
      }

}

ビュー

resources/views/samples/validation_sample_form.blade.php

@extends('layouts.default')

@section('title', $title)

@section('content')

<h1>{{ $title }}</h1>

<form method="post">
  @csrf
  <div>
    <label>
      名前:
      <input type="text" name="name">
    </label>
  </div>
  <div>
    <label>
      名前ふりがな:
      <input type="text" name="name_kana">
    </label>
  </div>
  <div>
    <label>
      ユーザー名アルファベットor数字:
      <input type="text" name="user_name">
    </label>
  </div>
  <div>
    <label>
      メールアドレス:
      <input type="text" name="email">
    </label>
  </div>
  <div>
    <label>
      年齢:
      <input type="text" name="age">
    </label>
  </div>
  <div>
    <label>
      生年月日:
      <input type="date" name="birthdate">
    </label>
  </div>
  <div>
    <label>
      住所:
      <input type="text" name="address">
    </label>
  </div>
  <div>
    <label>
      電話番号:
      <input type="text" name="phone">
    </label>
  </div>

  <div>
    <label>
      プライバシーポリシーに同意します:
      <input type="checkbox" name="policy">
    </label>
  </div>
  <div>
    <input type="submit" value="登録">
  </div>
</form>

@endsection

共通レイアウトファイル

<!DOCTYPE html>
<html lang="ja">
<head>
    <meta charset="UTF-8">
    <title>@yield('title')</title>
    <style>
        .header_nav {
            display: flex;
            list-style: none;
            padding-left: 0;
        }
        .header_nav li {
            margin-right: 30px;
        }
        /* エラーメッセージ用のスタイル */
        .error {
          color: red;
        }
    </style>
</head>
<body>
    @yield('header')

    {{-- エラーメッセージを出力 --}}
    @foreach($errors->all() as $error)
      <p class="error">{{ $error }}</p>
    @endforeach

    @yield('content')
</body>
</html>

image.png

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