Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Date validation in laravel 5

Tags:

php

laravel-5

I have a StartDate field and a EndDate field. Both may start today or in the future. It is possible to have them both start on the same day, including today. I have to make a validation for these. What I've done so far is this:

'StartDate'=>'required|date_format:Y/m/d|after:yesterday',
'EndDate'  => 'date_format:Y/m/d|after:yesterday',

What I don't know how to do is to validate true if both dates are equal as a second condition.

Anyone can help me, please ?

like image 547
Texas Avatar asked Sep 12 '15 16:09

Texas


People also ask

Is valid date in laravel?

How to validate date format in Laravel? If a user must provide a date in a specific format then you can validate it using the date_format validator. This validator supports all formats supported by PHP's DateTime class createFromFormat() method.

How do I validate a timestamp in laravel?

Just create a new validation rule in laravel to validate the timestamp... Validator::extend('isTimeStamp', function($attribute, $value, $parameters) { return ((string) (int) $value === $value) && ($value <= PHP_INT_MAX) && ($value >= ~PHP_INT_MAX); }); You can now use isTimeStamp validation rule to validate timestamp.

How do you add a validation rule in laravel?

You should add all your validation logic in the passes() function. It should return true or false based on the logic you have written in the function. The message() function returns a string that specifies the error message to be displayed in case the validation fails.

How does laravel validation work?

The validate method accepts an incoming HTTP request and a set of validation rules. If the validation rules pass, your code will keep executing normally; however, if validation fails, an exception will be thrown and the proper error response will automatically be sent back to the user.


1 Answers

This is how you can validate date:

'start_date'  =>  'required|date',
'end_date'    =>  'required|date|after_or_equal:start_date'
like image 87
Aqeel Avatar answered Sep 28 '22 11:09

Aqeel