How to validate time in laravel

不问归期 提交于 2020-06-24 10:59:04

问题


I want to validate time in Laravel. Ex:- I want that when user input the time between 8 PM to 10 PM then it will show the validation error. How can I achieve that in Laravel


回答1:


Use date_format rule validation

date_format:H:i

From docs

date_format:format

The field under validation must match the format defined according to the date_parse_from_format PHP function.




回答2:


Probably this code would work in your controller. However it won't validate times from different days (eg 9pm - 3am next day). time_start and time_end in this case should be provided as HH:mm but you can change it easily.

public function store(Illuminate\Http\Request $request)
{
    $this->validate($request, [
        'time_start' => 'date_format:H:i',
        'time_end' => 'date_format:H:i|after:time_start',
    ]);

    // do other stuff
}



回答3:


Create DateRequest and then add

<?php

namespace App\Http\Requests\Date;

use App\Http\Requests\FormRequest;


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


    /**
     * --------------------------------------------------
     * Get the validation rules that apply to the request.
     * --------------------------------------------------
     * @return array
     * --------------------------------------------------
     */
    public function rules(): array
    {
        return [

            'start_date' => 'nullable|date|date_format:H:i A',
            'end_date' => 'nullable|date|after_or_equal:start_date|date_format:H:i A'
        ];
    }
}



回答4:


Try This code

use Validator;
use Carbon\Carbon;


$timeHours = "7:00 PM";//change it to 8:00 PM,9:00 PM,10:00 PM  it works
$time = Carbon::parse($timeHours)->format('H');


$request['time'] = $time;
$validator = Validator::make($request->all(), [
    'time' => ['required','integer','between:20,22']
]);


 if ($validator->fails()) {
    dd($validator->errors());
}




回答5:


You probably should take a look to this bit of the documentations. A custom rule sounds like your way to go.




回答6:


In Lavavel 5.6:

Inside the file located in /app/Http/Requests/YourRequestValidation

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class YourRequestValidation 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 [
            'initial_time' => 'required',
            'end_time' => 'required',
            'end_time' => 'after:initial_time'
        ];
    }

    /**
     * Custom message for validation
     *
     * @return array
     */
    public function messages()
    {

        return [
            'initial_time.required' => 'Please, fill in the initial time',
            'end_time.required' => 'Please, fill in the end time.',
            'end_time.after' => 'The end time must be greater than initial time.'
        ];

    }

}



回答7:


$beginHour = Carbon::parse($request['hour_begin']);
        $endHour = Carbon::parse($request['hour_end']);
        if($beginHour->addMinute()->gt($endHour)){
            return response()->json([
                'message' => 'end hour should be after than begin hour',
            ], 400);
        }



来源:https://stackoverflow.com/questions/39467452/how-to-validate-time-in-laravel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!