How to After Today Validation in Laravel ?

Share Me
  •  
  •  
  •  
  • 1
  •  
  •  
  •  
  •  
  •  
  •  
  •  
    1
    Share

Today now in this blog, I will show you about how to validate date after today date validation in laravel 8 application. We know that Laravel provides us some very useful methods to validate date after to a given date.

Now so we can validate not enter today date before date then we can use this validation in laravel application. So the field under validation should be a value after a given date.

So we can use after:yesterday validation.

Now here I will give two solution of after today validation in laravel application.

Now here is a solution and i also give you a full example also with the route and view file too. Let’s start and see bellow solution:

Solution 1
public function storeAfterDateToday(Request $request)
{
    $request->validate([
        'date' => 'required|after:yesterday', //solution
    ]);
}
Solution 2
public function storeAfterDateToday(Request $request)
{
    $request->validate([
        'date' => 'required| after:' . date('Y-m-d'), //solution
    ]);
}

Route: routes/web.php

Route::get('after-date-today','FrontHomeController@afterDateToday');
Route::post('after-date-today','FrontHomeController@storeAfterDateToday')->name('after.date.today.store');

Controller : app/Http/controllers/FrontHomeController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class FrontHomeController extends Controller
{
    public function afterDateToday()
    {
        return view('afterDate');   
    }

    public function storeAfterDateToday(Request $request)
    {
        $request->validate([
            'date' => 'required|after:yesterday',
        ]);
        return redirect()->back();
    }
}   

View : resources/views/afterDate.blade.php

<!DOCTYPE html>
<html>
<head>
    <title>Laravel After Today Validation - codingspoint.com</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha256-L/W5Wfqfa0sdBNIKN9cG6QA5F2qx4qICmU2VgLruv9Y=" crossorigin="anonymous" />
    <script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.4.1/js/bootstrap.min.js" integrity="sha256-WqU1JavFxSAMcLP2WIOI+GB2zWmShMI82mTpLDcqFUg=" crossorigin="anonymous"></script>
</head>
<body>
    <div class="container">
        <div class="row">
            <div class="col-md-6 offset-3">
                <div class="card mt-5">
                    <div class="card-header bg-success">
                        <h3 class="text-white text-center"><strong>Laravel After Date Today Validation</strong></h3>
                    </div>
                    <div class="card-body">
                        @if(count($errors) > 0)
                            @foreach($errors->all() as $error)
                                <div class="alert alert-danger">{{ $error }}</div>
                            @endforeach
                        @endif
                        <form action="{{ route('after.date.today.store') }}" method="post">	
                            @csrf
                            <div class="form-group">
                                <label><b>Date :-</b></label>
                                <input type="date" name="date" class="form-control" value="{{ old('date') }}">
                            </div>
                            <div class="form-group text-center">
                                <button class="btn btn-success" type="submit">Save</button>
                            </div>
                        </form>		
                    </div>
                </div>		
            </div>
        </div>
    </div>
</body>
</html>

Read Also : Laravel 8 Eloquent Global Scope Tutorial Example

I hope it will help you. Also you can follow us on Facebook

About code chef

My name is Shahriar sagor. I'm a developer. I live in Bangladesh and I love to write tutorials and tips that will help to other Developer's. I am a big fan of PHP, Javascript, JQuery, Laravel, Codeigniter, VueJS, AngularJS and Bootstrap from the early stage.

View all posts by code chef →