Closed gaborfarkas0211 closed 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!
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".
So you want to throw an exception if the sent JSON is invalid?
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.
OK, can you do Try Catch
block inside the closure? and Trigger alert if invalid
Yes, thank you. Will it fixed in any future release?
Hey @pxpm @tabacitu
Need your two cents here.
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