Hi artisan, I’m going to share with you how to validate a custom date format in Laravel. In this example, you will see the use date validation rules that is provided by Laravel Let’s dive in:
- date
- date_format
- after:date
- after_or_equal:date
- before:date
- before_or_equal:date
1- Date Validation
1 2 3 4 5 6 7 8 9 | public function save(Request $request) { $request->validate([ 'date_of_birth' => 'date' ]); } |
2- Date Validation for date_format
1 2 3 4 5 6 7 8 | public function save(Request $request) { $request->validate([ 'date_of_birth' => 'date_format:m/d/Y' ]); } |
3- Date Validation for after
1 2 3 4 5 6 7 8 | public function save(Request $request) { $request->validate([ 'date_of_birth' => 'date_format:m/d/Y|after:tomorrow' ]); } |
4- Date Validation for after_or_equal
1 2 3 4 5 6 7 8 9 | public function save(Request $request) { $now=date('m/d/Y'); $request->validate([ 'start_date' => 'date_format:m/d/Y|after_or_equal:'.$now ]); } |
5- Date Validation for before
1 2 3 4 5 6 7 8 9 | public function save(Request $request) { $request->validate([ 'start_date' => 'date_format:m/d/Y|after:tomorrow', 'end_date' => 'date_format:m/d/Y|before:start_date' ]); } |
6- Date Validation for before_or_equal
1 2 3 4 5 6 7 8 9 | 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' ]); } |
I hope it can help you :).
Leave a Reply