To check if a date is in the future in Laravel validation, you can use the after
validation rule with the today
or now
parameter. Here’s an example of how to use the after
rule to validate if a date is in the future:
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
public function validateFutureDate(Request $request)
{
$validator = Validator::make($request->all(), [
'future_date' => 'required|date|after:today',
]);
if ($validator->fails()) {
// Handle validation error
// ...
}
// If validation passes, continue with the application logic
// ...
}
In this example, we’re using the after:today
validation rule to check if the future_date
field is a date in the future, relative to the current date. If the validation fails, we handle the validation error. If the validation passes, we can continue with the application logic.
Note that you can also use the now
parameter instead of today
to check if the date is in the future, relative to the current date and time:
$validator = Validator::make($request->all(), [
'future_date_time' => 'required|date|after:now',
]);
In this example, we’re using the after:now
validation rule to check if the future_date_time
field is a date and time in the future, relative to the current date and time. If the validation fails, we handle the validation error. If the validation passes, we can continue with the application logic.