Laravel 5.3 date validator: equal to or after start_date

Stefan Izdrail

Founder & Senior Architect · 2026-06-29

Laravel Company
Title: Extending Date Validation for Laravel 5.3 Events with Custom Rule or Alternative Methods Body:

In developing your application using Laravel 5.3, you may encounter the need to validate start and end dates for events while considering specific date conditions. One scenario is when end_date needs to be either equal to start_date or after it. While the built-in rules seem inadequate at first, there are multiple solutions to accomplish this goal.

Using Custom Rule

Validator::extend allows you to create new validation rules that can be easily used with Laravel validators. Here's an example for our current scenario:
Validator::extend('equal_or_after', function ($attribute, $value, $parameters) {
    // Check if the date is equal or after start_date
    return (is_null($parameters[0]) || strcmp(date('Y-m-d', strtotime($value)), $parameters[0]) >= 0);
});
Now, you can use this custom rule in your validator:
$validator = Validator::make($data, [
    'start_date'    => 'required|date',
    'end_date'      => ['equal_or_after:start_date'],
]);
This approach provides the most flexibility but requires additional work to implement and maintain.

Alternative Methods

Considering that validation is a fundamental part of Laravel's core functionality, there are several alternative ways to achieve the desired result without adding custom rules: 1. Split the logic into separate validators: - Create two independent validators for each date field. First validate start_date and then process end_date based on the results. - This approach is simple but not ideal in terms of code maintainability and testing. 2. Use third-party packages: - Several Laravel packages provide additional validation rules, such as Carbon's CarbonRules or Gourmet's CustomValidation. These packages may already include the functionality you need for start and end date validations. 3. Use a combination of rules: - Start by defining the rules for each field individually and then use the Laravel 5.3 sometimes() method to group them. Then, add conditional statements for checking specific scenarios (like equal or after date):
$validator = Validator::make($data, [
    'start_date' => 'required|date',
]);

if ($validator->fails()) {
    $rules = ['end_date' => 'after:start_date'];
} else {
    $rules = ['end_date' => 'equal:start_date'];
}

$validator2 = Validator::make($data, $rules);
- This approach offers more flexibility and allows you to create multiple scenarios. However, it can become cumbersome if there are many date conditions to handle. In conclusion, there are several ways to achieve the desired result in Laravel 5.3 without adding custom rules. Your choice will depend on your project's specific needs and preferences regarding code maintainability and scalability. If you aim for a cleaner solution, consider using third-party packages or alternative methods instead of creating custom rules. Always remember that keeping your code well structured and easy to read is essential for maintaining a successful Laravel application.