What would be a valid year? 1 is a year. So is 20341.
Sep 20, 2016
8
Level 3
How to validate date with only year input
Greetings Pros,
I have input box that will only accept year date. Now i want to know how can i validate that the inputted year is a valid year. I'm using Request to validate inputs but i can't validate the inputted year using date and date_format.
I tried this
'pricing_date' => 'required|date_format:Y',
'pricing_date' => 'required|date',
Thanks!
Level 32
@jake2025 in the boot method of AppServiceProvider (import carbon)
Validator::extend('date_greater_than', function($attribute, $value, $parameters, $validator) {
$inserted = Carbon::parse($value)->year;
$since = $parameters[0];
return $inserted >= $since && $inserted<= Carbon::now()->year;
});
in your rule
'pricing_date' => 'required|date_greater_than:1900',
4 likes
Please or to participate in this conversation.