How can I set comparison datetime in validation laravel?

Success Man picture Success Man · Aug 1, 2017 · Viewed 8.4k times · Source

I use laravel 5.3

My laravel validation like this :

<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class AddCartRequest extends FormRequest
{
    public function rules()
    {
        return [
            'id'=>'required|numeric',
            ...
            'request_date'=>'required'
        ];
    }
}

If value of request_date is

01-08-2017 13:00:00

I want to add condition like this :

If request date < now + 5 hour, then it will display message : "Request date must be larger 5 hours from now"

How can I do it?

Answer

gview picture gview · Aug 1, 2017

I think you can use after:date

The field under validation must be a value after a given date. The dates will be passed into the PHP strtotime function.

You simply need to create the right date string using something like this:

'request_date'=>'after:'.date(DATE_ATOM, time() + (5 * 60 * 60)),

Seems that you can also chain these rules like this:

'request_date' => 'required|date_format:Y-m-d H:i:s|after:5 hours'