Laravel-Backpack / CRUD

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

[Bug] Pro: date_range filter undefined array key from #5519

Closed gaborfarkas0211 closed 4 months ago

gaborfarkas0211 commented 4 months ago

Bug report

What I did

I used the Pro package's date_range filter type. When I select a range, the filter works properly, but when I modify the query params to make a mistake in the json structure.

What I expected to happen

The filter handle the json encoding properly and handle the from - to keys in the array.

What happened

I have got an error: undefined array key "from".

What I've already tried to fix it

There is no way for me to fix it unless I override the filter in my project.

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:

PHP VERSION:

8.3.4

LARAVEL VERSION:

11.7.0.0

BACKPACK PACKAGE VERSIONS:

backpack/pro: 2.2.0

karandatwani92 commented 4 months ago

Hey @gaborfarkas0211

I'm regularly using the date_range filter in my projects and facing no issues with it. Here is how I'm using it.

CRUD::addFilter(
[
   'type'  => 'date_range',
   'name'  => 'from_to',
   'label' => 'Date range'
],
false,
   function ($value) {
      $dates = json_decode($value);
      $this->crud->addClause('where', 'created_at', '>=', $dates->from);
      $this->crud->addClause('where', 'created_at', '<=', $dates->to . ' 23:59:59');
   }
);

If you still face the issue, share your code so i can help!

gaborfarkas0211 commented 4 months ago

Hi @karandatwani92,

Thanks for your reply. It's not about the code itself, use the above code, set a filter, and then modify the URL's query of this filter. e.g. the filtered URL query looks like this: created_at=%7B"from"%3A"2024-05-16+13%3A50%3A54"%2C"to"%3A"2024-05-16+13%3A50%3A54"%7D

If I remove the %7B before "from", it will be an invalid JSON. After hitting enter, the json_decode($filter->currentValue) result will be an empty array. created_at="from"%3A"2024-05-16+13%3A50%3A54"%2C"to"%3A"2024-05-16+13%3A50%3A54"%7D

This will cause this error: Undefined array key "from".

karandatwani92 commented 4 months ago

So you want to throw an exception if the sent JSON is invalid?

gaborfarkas0211 commented 4 months ago

No, I just want to handle the invalid JSON $dates result and set a default value e.g. null when the json_decode result is an empty array.

$dates['from'] ?? null $dates['to'] ?? null

So the user can't make a 500 server error, and the filter will not work either.

The issue is the same with the simple range filter.

karandatwani92 commented 4 months ago

OK, can you do Try Catch block inside the closure? and Trigger alert if invalid

gaborfarkas0211 commented 4 months ago

Yes, thank you. Will it fixed in any future release?

karandatwani92 commented 4 months ago

Hey @pxpm @tabacitu

Need your two cents here.