13
2

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 で 開始と終了 時刻 でのバリデーション

Posted at

Laravel6を使用して動作を確認しています。

#画面

form.blade.php
<input type="text" placeholder="00:00" name="start_time" class="form-control">
@error('start_time') {{ $message }} @enderror
<input type="text" placeholder="00:00" name="end_time" class="form-control">
@error('end_time') {{ $message }} @enderror

SC000002.JPG

#バリデーション
FormRequestで行います

php artisan make:make:request TimeRequest
TimeRequest.php
<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class TimeRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'start_time' => 'required|date_format:H:i|',
            'end_time' => 'required|date_format:H:i|after:start_time',
        ];
    }
}


これで適当なControllerのメソッドへformをpostするようにして、引数にTimeRequestを指定します

TekitoController
<?php

namespace App\Http\Controllers;

use App\Http\Requests\TimeRequest;

class TekitoControllerextends Controller
{
    public function some(TimeRequest $request)
    {
        //バリデーション済み$requestが渡ってくる
    }
}

(日本語化済みの表示サンプル)
SC000003.JPG

13
2
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
13
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?