hekmatinasser / verta

تبدیل تاریخ میلادی به شمسی برای لاراول و کار با تاریخ جلالی و اعتبارسنجی تاریخ
https://hekmatinasser.github.io/verta/
MIT License
582 stars 77 forks source link

Help for Laravel Validation #142

Closed sinar93 closed 1 year ago

sinar93 commented 1 year ago

Hi

I'm trying to do a simple Laravel validation on two date inputs:

       $attributes = request()->validate([
            'sale_from' => 'jdate:Y/m/d H:i:s',
            'sale_to' => 'jdate_after:sale_from,Y/m/d H:i:s'
        ]);

sale_from is the date which sale is started and sale_to is when sale ends.

The problem here is no matter what I enter in date inputs (correct and incorrect) I always get the error message: تاریخ پایان تخفیف باید بعد از sale_from باشد.

I have updated the attributes array in validation translation file:

        'sale_from' => 'تاریخ شروع تخفیف',
        'sale_to' => 'تاریخ پایان تخفیف',

So it seems weird that it doesn't translate sale_from. I think the problem is that it can not read value of sale_from. I have also tried jdatetime instead of jdate:

       $attributes = request()->validate([
            'sale_from' => 'jdatetime:Y/m/d H:i:s',
            'sale_to' => 'jdatetime_after:sale_from,Y/m/d H:i:s'
        ]);

I would like to know if you could help me to figure it out. Thanks in advance

hekmatinasser commented 1 year ago

hi jdate check part date form input date time ex: 1401/07/25 and jdatetime for date and time together
please check parameters request sale_from and sale_to

dd(validator([ 'sale_from' => '1401/07/25 12:20:30', 'sale_to' => '1401-06-31 12:20:30', ],[ 'sale_from' => 'jdatetime:Y/m/d H:i:s', 'sale_to' => 'jdatetime_after:'. request('sale_from') .',Y/m/d H:i:s' ]) ->errors() ->messages());

array:1 [ "sale_to" => array:1 [ 0 => "تاریخ پایان تخفیف باید بعد از باشد." ] ]

sinar93 commented 1 year ago

Thank you for your reply,

These are the datetime inputs in request: image And this is the die dump of validator: image Which is incorrect I'm really sorry to bother you, but it always return "تاریخ پایان تخفیف باید بعد از ... باشد" no matter what.

sinar93 commented 1 year ago

I'm bypassing the problem this way for now:

        request()->validate([
            'sale_from' => 'jdatetime:Y/m/d H:i:s',
            'sale_to' => 'jdatetime:Y/m/d H:i:s'
        ]);
        request()->merge([
            'sale_from' => Verta::parse(request('sale_from'))->datetime()->format('Y/m/d H:i:s'),
            'sale_to' => Verta::parse(request('sale_to'))->datetime()->format('Y/m/d H:i:s'),
        ]);
        $attributes = request()->validate([
            'sale_from' => 'date_format:Y/m/d H:i:s',
            'sale_to' => 'date_format:Y/m/d H:i:s|after:sale_from'
        ]);

(1) Validating Jalali dates in request (2) Merging Jalali dates with Verta to Gregorian (3) Checking if dates are in correct order using|after:sale_from And it works fine for now, I thought it might help.

Best