Vikash
Vikash

Reputation: 3561

How to validate time in laravel

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

Upvotes: 58

Views: 91825

Answers (6)

Anil Stha
Anil Stha

Reputation: 581

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'
        ];
    }
}

Upvotes: 4

Alexandre Ribeiro
Alexandre Ribeiro

Reputation: 1454

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.'
        ];

    }

}

Upvotes: 1

Chathuranga Tennakoon
Chathuranga Tennakoon

Reputation: 2189

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.

Upvotes: 105

Skysplit
Skysplit

Reputation: 1943

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
}

Upvotes: 90

Parithiban
Parithiban

Reputation: 1666

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());
}

enter image description here

Upvotes: 1

UP.
UP.

Reputation: 187

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

Upvotes: 0

Related Questions