How to validate custom Date Format with Laravel validator?

How to validate custom Date Format with Laravel validator?

In this Laravel PHP Tutoria, I will let you know how to validate date with specified date format using Laravel validation rules.

Laravel always introduce some awesome features with its updates and date validation is one of them.

You can validate date after, date_format, after_or_equal:date, before:date, before_or_equal:date etc.

Laravel date validation rules support all format supported by PHP's Date class.

In this example, you will see the use of following date validation rules that is provided by Laravel:

  • date
  • date_format
  • after:date
  • after_or_equal:date
  • before:date
  • before_or_equal:date
Date Validation
public function save(Request $request)
{
   
    $request->validate([        
        'date_of_birth' => 'date'
    ]);
  
}
Date Validation for date_format
public function save(Request $request)
{
   
    $request->validate([        
        'date_of_birth' => 'date_format:m/d/Y'
    ]);
  
}
Date Validation for after
public function save(Request $request)
{
   
    $request->validate([        
        'start_date' => 'date_format:m/d/Y|after:tomorrow'
    ]);
  
}
Date Validation for after_or_equal
public function save(Request $request)
{
   $now=date('m/d/Y');
    $request->validate([        
        'start_date' => 'date_format:m/d/Y|after_or_equal:'.$now
    ]);
  
}
Date Validation for before
public function save(Request $request)
{
   
    $request->validate([        
        'end_date' => 'date_format:m/d/Y|before:start_date',
        'start_date' => 'date_format:m/d/Y|after:tomorrow'
    ]);
  
}
Date Validation for before_or_equal
public function save(Request $request)
{
   
    $request->validate([        
        'end_date' => 'date_format:m/d/Y|before_or_equal:start_date',
        'start_date' => 'date_format:m/d/Y|after:tomorrow'
    ]);
  
}

Sometime we develop an application having start and end date functionality for any reservation and in this case, there should be validation like end date must be after start date.

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

Phone: (+91) 8800417876
Noida, 201301
sakarya escort sakarya escort bayan sakarya escort serdivan escort sakarya escort sakarya escort serdivan escort
karasu escort akyazı escort serdivan escort akyazı escort ferizli escort sapanca escort serdivan escort söğütlü escort sakarya escort izmit escort arifiye escort erenler escort hendek escort kocaali escort pamukova escort taraklı escort geyve escort