0

I want to validate my backend code with the following data.I passed date as $request->get('date_from'),$request->get('date_to') and time as $request->get('time_from'), $request->get('time_to') from my angular frontend and I convert date time as follows.

$dateTime_from=date('Y-m-d',strtotime($request->get('date_from'))).' '.$request->get('time_from');
$dateTime_to=date('Y-m-d',strtotime($request->get('date_to'))).' '.$request->get('time_to');

Now I want to validate DateTime with laravel backend validations. dateTime_from should be less than dateTime_to.How can write down that code inside validator?

$this->validate($request, [
            'vehicle_id'=>'required',
            'time_to'=>'required',
            'event_id'=>'required',

        ]);

2 Answers 2

1

You can use the after validation rule.

$this->validate($request, [
  'vehicle_id' => 'required',
  'date_to'    => 'required|after:date_from',
  'event_id'   => 'required'
]);
Sign up to request clarification or add additional context in comments.

4 Comments

I want to compare $dateTime_from with $dateTime_to is there any way to do that?
@GayalSeneviratne Isn't that what he has posted?
@Cerlin not only date_from should be less than $date_to. For instance, let's say if frontend passed same $date_from and $date_to. I want to clarify with the time too. for example $dateTime_from=2019-1-5 08:30:00 $dateTime_to=2019-1-5 07:00:00 then this instance $dateTime_from greater tha the $dateTime_to. this is the issue I want to handle
Laravel uses php's strtotime method. So time will also be considered while validating. So please try the solution mentioned above
0

https://laravel.com/docs/5.8/validation#rule-after

you can use the after rule like follows

'date_to' => 'required|date|after:date_from'

Instead of passing a date string to be evaluated by strtotime, you may specify another field to compare against the date:

Also, you have rule-before as well

EDIT I think after rule takes the time into consideration as well, but not sure.

And you have really complex validation to do, better write a custom rule class or a closure to handle it for you

'date_to' => [ 'required', 
               'date',
               function ($attribute, $value, $fail) {
                  if (strtotime($value) <= strtotime($request->date_from) {
                  $fail(':attribute needs to be higher than date_from!'); // or whatever mesage ou need to send
               }
             ]

2 Comments

not only date_from should be less than $date_to. For instance, let's say if frontend passed same $date_from and $date_to. I want to clarify with the time too. for example $dateTime_from=2019-1-5 08:30:00 $dateTime_to=2019-1-5 07:00:00 then this instance $dateTime_from greater tha the $dateTime_to. this is the issue I want to handle
Edited answer, please check

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.