Laravel Tutorials

Building Powerful Date Validation with Laravel's Date Rule

Published
Building Powerful Date Validation with Laravel's Date Rule image

Laravel introduces a fluent Date rule that transforms how you validate dates in your applications. This chainable interface simplifies complex date validation requirements into readable, maintainable code.

The basic implementation demonstrates the rule's flexibility:

use Illuminate\Validation\Rules\Date;

public function rules()
{
    return [
        'start_date' => [
            'required',
            new Date  // Basic date validation
        ],
        'end_date' => [
            'required',
            (new Date)
                ->after('start_date')
                ->before('2025-01-01')
        ],
        'birth_date' => [
            'required',
            (new Date)
                ->format('d/m/Y')
                ->beforeToday()
        ]
    ];
}

Here's how this rule streamlines validation in a reservation system:

class ReservationController extends Controller
{
    public function store(Request $request)
    {
        $validated = $request->validate([
            'check_in' => [
                'required',
                (new Date)
                    ->format('Y-m-d')
                    ->afterOrEqual('today')
                    ->before(now()->addMonths(6))
            ],
            'check_out' => [
                'required',
                (new Date)
                    ->format('Y-m-d')
                    ->after('check_in')
            ],
            'special_request_by' => [
                'nullable',
                (new Date)
                    ->format('Y-m-d')
                    ->between('today', 'check_in')
            ],
            'guest_birthday' => [
                'nullable',
                (new Date)
                    ->format('m/d/Y')
                    ->beforeOrEqual('today')
                    ->after(now()->subYears(120))
            ]
        ]);

        Reservation::create($validated);
        
        return redirect()->route('reservations.confirmation');
    }
}

The Date rule's chainable methods create an expressive syntax that makes even complex date validation requirements easy to understand and maintain.

Harris Raftopoulos photo

Senior Software Engineer • Staff & Educator @ Laravel News • Co-organizer @ Laravel Greece Meetup

Sponsored

acquaintsoft logo
Acquaint Softtech

Hire Laravel developers with AI expertise at $20/hr. Get started in 48 hours.

Visit Acquaint Softtech

The latest

View all →
pgjumbo: A PostgreSQL Client for macOS, Windows, and Linux image

pgjumbo: A PostgreSQL Client for macOS, Windows, and Linux

Read article
Laranon: Reversible PII Anonymization for Laravel and LLMs image

Laranon: Reversible PII Anonymization for Laravel and LLMs

Read article
PHP 8.6 is released!  image

PHP 8.6 is released!

Read article
Laravel Warrant: Row-Level Permissions With Eloquent image

Laravel Warrant: Row-Level Permissions With Eloquent

Read article
JSON Schema Validation Rules and a RetryFor Attribute in Laravel 13.43 image

JSON Schema Validation Rules and a RetryFor Attribute in Laravel 13.43

Read article
Durable Workflow: Long-Running Workflows for Laravel image

Durable Workflow: Long-Running Workflows for Laravel

Read article