How to use Date Format Validation in Laravel?

  • 13-04-2022
  • 1094
  • Laravel 6
  • Haresh Chauhan

Hi, Artisan

Hello my Dear, friends in this post we will learn how to use Laravel Date validation many users ask me how can we use the Date validation rules in the Laravel Framework.

So, Friends this post we will discuss the Date Validation rules of the PHP Laravel Framework and as well as we are know how we can use these validation rules.

Here I will give some tips step by step if you want to use date validation then you can follow the below steps and set date validation rules into our fresh Laravel framework application.

As we know Laravel 6 provide date validation rules list are below You can see.

  • date
  • date_format
  • after:date
  • after_or_equal:date
  • before:date
  • before_or_equal:date
How to use Date Format Validation in Laravel?

Now we will show one by one with example so, let's see bellow date format example into controller:

1) date Validation:

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

2) date_format Validation:

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

3) after Validation:

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

4) after_or_equal Validation:

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

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

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

We must use start date and end date validation as like bellow example:

Step 7 : Start Date and End Date Example:

Laravel 6 validation with error message

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

I hope this post can be help you.

Thanks...


We always thanks to you for reading our blogs.


dharmesh-image

Dharmesh Chauhan

(Swapinfoway Founder)

Hello Sir, We are brothers origin from Gujarat India, Fullstack developers working together since 2016. We have lots of skills in web development in different technologies here I mention PHP, Laravel, Javascript, Vuejs, Ajax, API, Payment Gateway Integration, Database, HTML5, CSS3, and Server Administration. So you need our service Please Contact Us

haresh-image

Haresh Chauhan

(Co-Founder)


We Are Also Recommending You :