How to validate custom Date Format with Laravel validator?

How to validate custom Date Format with Laravel validator?

In this post we will give you information about How to validate custom Date Format with Laravel validator?. Hear we will give you detail about How to validate custom Date Format with Laravel validator?And how to use it also give you demo for it if it is necessary.

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'
    ]);
   
}



How To Create Custom Validation Rules With Laravel 5

Label :

PHP

Laravel PHP Framework

How To

MVC

Web Development

Hope this code and post will helped you for implement How to validate custom Date Format with Laravel validator?. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

Leave a Comment

Your email address will not be published. Required fields are marked *

  +  62  =  71

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US