In this post, I would like to show you date format validation in Laravel application, You should always validate forms to ensure that you receive data in the good format.
Date validation Rules
Here is the list of validation rules
How to use validation rules
You can validate date with
date validation rules. In order to satisfy those two validation rules, the field must be a valid date according to the
strtotime PHP function. With
date_format:format method you can set the preferred date format, and the filed value must match the given format.
Here is the examples with both -
// with date $rules = [ 'start_date' => 'date', ]; // with date_format $rules = [ 'start_date' => 'date_format:d/m/Y', ];
after:date : Used to ensure that a field contains valid date that occurs after provided date. Here’s an example.
// for after tomorrow $rules = [ 'start_date' => 'date_format:d/m/Y|after:tomorrow', ]; // for after specific date $rules = [ 'start_date' => 'date_format:d/m/Y|after:3/13/2014', ]; // after start date $rules = [ 'start_date' => 'date_format:d/m/Y', 'end_date' => 'date_format:d/m/Y|after:start_date', ];
You could also use
after_or_equal:date same as shown above, the only difference is that the field under validation must be a value after or equal to the given date
You can even validate preceding dates with
before_or_equal:date respectively. You can use this validation rules also just like above shown.