Laravel after_or_equal Date Validation Example

Laravel 8 , Laravel 7 , Laravel , Laravel 6

Nicesnippets

7698

03-12-2020


Laravel after_or_equal Date Validation Example

Hi All,

In this article, i will show you how to validate after_or_equal date validation in laravel. I am going to learn you after_or_equal date validation example in laravel. We will talk about laravel after_or_equal date validation example.

The field under validation must be a value after or equal to the given date. For more information, see the after rule. The field under validation must be a value after or equal a given date. The dates will be passed into the strtotime PHP function:

In this example i will show you start date after or equal date validation in laravel. Here i will give you solution and full example So let's see the bellow example.

Solution


$request->validate([

'start_date' => 'required',

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

]);

Add Route

Route::get('after-equal-date',[ValidationController::class,'create']);

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

Create ValidationController

In this step, you can create a new controller as ValidationController So let's open terminal run bellow artisan command:

php artisan make:controller ValidationController

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class ValidationController extends Controller

{

public function afterEqualDate()

{

return view('validation.afterEqualDate');

}

public function afterEqualDateStore(Request $request)

{

$request->validate([

'start_date' => 'required',

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

]);

}

}

Create View file

<!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>

It will help you...


Recommended Posts