Search code examples
laraveldatetimedatetime-formatlaravel-7laravel-validation

laravel-7 : date_format jalali calender validation


I'm getting a DateTime format from user input:

"Y/m/d H:i"

this is my validation:

$request->validate([
            'deadline' => 'required|date_format:Y/m/d H:i'
        ]);

but the problem is I'm using Jalali calender so 6th month of the jalali have 31 days. and user input will be like this:

1400/06/31

and now validator will throw an error because there are no 31 days in the 6th month of the Georgian calender.

how should I validate this date?


Solution

  • try to use a rule over existed validation of laravel,

    php artisan make:rule [JalaliDateValidation]
    

    if you are using morilog/jalali try to use checkdate function

    // Check jalali date
    \Morilog\Jalali\CalendarUtils::checkDate(1391, 2, 30, true); // true
    
    // Check jalali date
    \Morilog\Jalali\CalendarUtils::checkDate(2016, 5, 7); // false
    
    // Check gregorian date
    \Morilog\Jalali\CalendarUtils::checkDate(2016, 5, 7, false); // true