Laravel Community Tools by Tighten

Validation and behavior of date, date_format, before and after rules

Submitted by Lelectrolux - 11 years ago

Let's talk about date validation and how theses 4 rules works, as their behaviour is even more awesome than documented.

What is know :
- date, before and after uses strtotime PHP native function.
- date_format uses date_parse_from_format PHP native function

But how do we validate a date after today in a custom format ?
And what to do to compare two form fields ?

What is not documented is that before and after rules check if a date_format rule exists, and will use this format if needed.

Example: I have french formated dates (31/01/2015), and two fields, start_date and end_date. start_date must be after tomorrow, and end_date after start_date

$rules = [
    'start_date'        => 'date_format:d/m/Y|after:tomorrow',
    'end_date'          => 'date_format:d/m/Y|after:start_date',
];