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
#バリデーション
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が渡ってくる
}
}