Laravel-Backpack / CRUD

Build custom admin panels. Fast!
https://backpackforlaravel.com
MIT License
3.04k stars 885 forks source link

[Bug] field date_range select ranges invalid date #5443

Closed adriallongarriu closed 5 months ago

adriallongarriu commented 6 months ago

Bug report

What I did

Use ranges options in a date_range. I wanted the same options as a filter an copy the seme config as vendor\backpack\pro\resources\views\filters\date_range.blade.php

My code:

$this->crud->field([
                'name'  => 'start_date,end_date',
                'label' => 'Event Date Range',
                'type'  => 'date_range',
                'date_range_options' => [
                    'alwaysShowCalendars' => true,
                    'ranges' => [
                        trans('backpack::crud.today') =>  [Carbon::now()->startOfDay()->toDateTimeString(), Carbon::now()->endOfDay()->toDateTimeString()],
                        trans('backpack::crud.yesterday') => [Carbon::now()->subDay()->startOfDay()->toDateTimeString(), Carbon::now()->subDay()->endOfDay()->toDateTimeString()],
                        trans('backpack::crud.last_7_days') => [Carbon::now()->subDays(6)->startOfDay()->toDateTimeString(), Carbon::now()->toDateTimeString()],
                        trans('backpack::crud.last_30_days') => [Carbon::now()->subDays(29)->startOfDay()->toDateTimeString(), Carbon::now()->toDateTimeString()],
                        trans('backpack::crud.this_month') => [Carbon::now()->startOfMonth()->toDateTimeString(), Carbon::now()->endOfMonth()->toDateTimeString()],
                        trans('backpack::crud.last_month') => [Carbon::now()->subMonth()->startOfMonth()->toDateTimeString(), Carbon::now()->subMonth()->endOfMonth()->toDateTimeString()]
                    ],
                    'locale' => [
                        'firstDay' => 1,
                        'format' => config('backpack.base.default_date_format'),
                        'applyLabel'=> trans('backpack::crud.apply'),
                        'cancelLabel'=> trans('backpack::crud.cancel'),
                        'customRangeLabel' => trans('backpack::crud.custom_range')
                    ]
                ]
            ]);

What I expected to happen

Be able to select a predefined date range

What happened

On clicking any range the date is invalid and it's impossible to select any date image

What I've already tried to fix it

I override the date_range.blade.php and add the folowing code:

$ranges = $configuration.ranges;
$configuration.ranges = {};

  //if developer configured ranges we convert it to moment() dates.
  for (var key in $ranges) {
      if ($ranges.hasOwnProperty(key)) {
          $configuration.ranges[key] = $.map($ranges[key], function($val) {
              return moment($val);
          });
      }
  }

Is it a bug in the latest version of Backpack?

After I run composer update backpack/crud the bug... is it still there? YES

Backpack, Laravel, PHP, DB version

When I run php artisan backpack:version the output is: image

karandatwani92 commented 6 months ago

Hey @adriallongarriu

The problem was the format.

I created ranges with the following format:

CRUD::field([
   'name'  => 'start_date,end_date',
   'label' => 'Event Date Range',
   'type'  => 'date_range',
   'date_range_options' => [
     'ranges' => [
         trans('backpack::crud.today') => [\Carbon\Carbon::now()->startOfDay()->format('m/d/Y'), \Carbon\Carbon::now()->startOfDay()->format('m/d/Y')],
         trans('backpack::crud.yesterday') => [\Carbon\Carbon::now()->subDay()->startOfDay()->format('m/d/Y'), \Carbon\Carbon::now()->subDay()->endOfDay()->format('m/d/Y')],
      ]
   ]
]);
adriallongarriu commented 6 months ago

Hey @adriallongarriu

The problem was the format.

I created ranges with the following format:

CRUD::field([
   'name'  => 'start_date,end_date',
   'label' => 'Event Date Range',
   'type'  => 'date_range',
   'date_range_options' => [
     'ranges' => [
         trans('backpack::crud.today') => [\Carbon\Carbon::now()->startOfDay()->format('m/d/Y'), \Carbon\Carbon::now()->startOfDay()->format('m/d/Y')],
         trans('backpack::crud.yesterday') => [\Carbon\Carbon::now()->subDay()->startOfDay()->format('m/d/Y'), \Carbon\Carbon::now()->subDay()->endOfDay()->format('m/d/Y')],
      ]
   ]
]);

Tested and it works but i don't undestend why. Why need this specified date format and why in the filters\date_range.blade.php is using other format.

I checked https://www.daterangepicker.com/ and not specified a format. And is using moment(). image image

I think the correct solution would be to implement the same change that exists in filters\date_range.blade.php and transform the dates to moment(). It does not imply a new dependency because it is already being used inside backpack and it will be more robust as it works with more date formats.

adriallongarriu commented 5 months ago

@pxpm could you take a look at this issue. I believe it's a bug and the correct solution will be implementing the same change as it is done in the \filters\date_range.blade.php and transform the dates to moment js. Plus the translation is still the wrong.

I would have made a pull request but this file is in the pro

pxpm commented 5 months ago

Hey @adriallongarriu hope to find you well 🙏

Thanks for the ping, and sorry for the delay, we have been super busy adding Laravel 11 support to all our packages.

I've released PRO 2.1.11 with the fix, let me know if you are still experiencing issues 🙏

Cheers