Laravel Before Date Validation Example

10-Apr-2023

.

Admin

Laravel Before Date Validation Example

Hi Guys,

In this blog, i am going to learn you how to validate before date validation in laravel application. We will talk about laravel before date validation example. This tutorial will give you before date validation example in laravel. In this article i would like to share with you before date validation with date format in laravel.

The field under validation must be a value preceding the given date. The dates will be passed into the PHP strtotime function in order to be converted into a valid DateTime instance. In addition, like the after rule, the name of another field under validation may be supplied as the value of date.

Here i will give you solution and full example for laravel before date validation example So let's see the bellow solution and example:

Solution 1


$request->validate([

'start_date' => 'required',

'end_date' => 'required|date|before:start_date'

]);

Solution 2 : Other Date Format

$request->validate([

'start_date' => 'required',

'end_date' => 'required|date_format:d/m/Y|before:start_date'

]);

Full Example :

Add Route

use App\Http\Controllers\ValidationController;

Route::get('before-equal-date',[ValidationController::class,'beforeEqualDate']);

Route::post('store-before-equal-date',[ValidationController::class,'beforeEqualDateStore'])->name('store-after-equal-date');

Controller : app/Http/Controllers/ValidationController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class ValidationController extends Controller

{

public function beforeEqualDate()

{

return view('validation.beforeEqualDate');

}

public function beforeEqualDateStore(Request $request)

{

$request->validate([

'start_date' => 'required',

'end_date' => 'required|date|before:start_date'

]);

}

}

View : resources/views/validation/beforeEqualDate.php

<!DOCTYPE html>

<html>

<head>

<title>Laravel after_or_equal Date Validation Example</title>

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.5.3/css/bootstrap.min.css" integrity="sha512-oc9+XSs1H243/FRN9Rw62Fn8EtxjEYWHXRvjS43YtueEewbS6ObfXcJNyohjHqVKFPoXXUxwc+q1K7Dee6vv9g==" crossorigin="anonymous" />

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js" integrity="sha512-bLT0Qm9VnAYZDflyKcBaQ2gg0hSYNQrJ8RilYldYQ1FxQYoCLtUjuuRuZo+fjqhx/qtq/1itJ0C2ejDxltZVFg==" crossorigin="anonymous"></script>

<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.5.3/js/bootstrap.min.js" integrity="sha512-8qmis31OQi6hIRgvkht0s6mCOittjMa9GMqtK9hes5iEQBQE/Ca6yGE5FsW36vyipGoWQswBj/QBm2JR086Rkw==" crossorigin="anonymous"></script>

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.9.0/css/bootstrap-datepicker.min.css" integrity="sha512-mSYUmp1HYZDFaVKK//63EcZq4iFWFjxSL+Z3T/aCt4IO9Cejm03q3NKKYN6pFQzY0SBOr8h+eCIAZHPXcpZaNw==" crossorigin="anonymous" />

<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.9.0/js/bootstrap-datepicker.min.js" integrity="sha512-T/tUfKSV1bihCnd+MxKD0Hm1uBBroVYBOYSk1knyvQ9VyZJpc/ALb4P0r6ubwVPSGB2GvjeoMAJJImBG12TiaQ==" crossorigin="anonymous"></script>

</head>

<body>

<div class="container mt-5">

<div class="row">

<div class="col-md-8 offset-2 mt-5">

<div class="card">

<div class="card-header bg-info text-white">

<h3><strong>Laravel after_or_equal Date Validation Example</strong></h3>

</div>

<div class="card-body">

<form action="{{ route('store-after-equal-date') }}" method="post">

@csrf

<div class="form-group">

<label>Start Date :</label>

<input class="datepicker form-control" name="start_date" value="{{ old('start_date') }}">

@if($errors->has('start_date'))

<span class="text-danger">{{ $errors->first('start_date') }}</span>

@endif

</div>

<div class="form-group">

<label>End Date :</label>

<input class="datepicker form-control" name="end_date" value="{{ old('end_date') }}">

@if($errors->has('end_date'))

<span class="text-danger">{{ $errors->first('end_date') }}</span>

@endif

</div>

<div class="form-group">

<button class="btn btn-success btn-sm">Save</button>

</div>

</form>

</div>

</div>

</div>

</div>

</div>

<script type="text/javascript">

$('.datepicker').datepicker({

autoclose: true

});

$('.end-date').datepicker({

autoclose: true

});

</script>

</body>

</html>

Run Your Project

Now we are ready to run our example so run bellow command for quick run:

php artisan serve

Now you can open bellow URL on your browser:

http://localhost:8000/before-equal-date

It will help you....

#Laravel

#Laravel 6