date_calendar

How to validate Date in Laravel – Date Validation

Hello Geek, In this example, we are going to learn “How to validate dates in Laravel” (Date Validation). Laravel provides date validation such as date after, date format, after or equal: date, before: date, before or equal: date, and so on. So, in this example, I’ll show you how to use validation date after or equal today in Laravel, how to use date format validation in Laravel, how to use validation date before or equal today in Laravel, and so on.
Date format validation can be added to projects written in Laravel 6, 7, 8, and 9.
As you can see, I listed the following date validation that Laravel provides by default.

Example 1: date Validation

public function store(Request $request)
{
   
    $request->validate([
        'name' => 'required',
        'email' => 'required|email',
        'birth_date' => 'date'
    ]);
  
}

Example 2: date_format Validation

public function store(Request $request)
{
   
    $request->validate([
        'name' => 'required',
        'email' => 'required|email',
        'birth_date' => 'date_format:m/d/Y'
    ]);
  
}

Example 3: after Validation

public function store(Request $request)
{
   
    $request->validate([
        'name' => 'required',
        'email' => 'required|email',
        'start_date' => 'date_format:m/d/Y|after:tomorrow'
    ]);
  
}

Example 4: after_or_equal Validation

public function store(Request $request)
{
    $todayDate = date('m/d/Y');
   
    $request->validate([
        'name' => 'required',
        'email' => 'required|email',
        'start_date' => 'date_format:m/d/Y|after_or_equal:'.$todayDate
    ]);
  
}

Example 5: before Validation

public function store(Request $request)
{
   
    $request->validate([
        'name' => 'required',
        'email' => 'required|email',
        'end_date' => 'date_format:m/d/Y|before:start_date',
        'start_date' => 'date_format:m/d/Y|after:tomorrow'
    ]);
   
}

Example 6: before_or_equal Validation

public function store(Request $request)
{
   
    $request->validate([
        'name' => 'required',
        'email' => 'required|email',
        'end_date' => 'date_format:m/d/Y|before_or_equal:start_date',
        'start_date' => 'date_format:m/d/Y|after:tomorrow'
    ]);
  
}

Example 7: start and end date validation in laravel

public function store(Request $request)
{
   
    $request->validate([
        'name' => 'required',
        'email' => 'required|email',
        'start_date' => 'date_format:m/d/Y',
        'end_date' => 'date_format:m/d/Y|after:start_date'
    ]);
   
}

Best wishes Nerd.

Leave a Reply