Multi value delimiter remove spaces between keywords #514
Replies: 2 comments
-
Hi, thanks for opening this discussion. I don't think that normalizing and sanitizing user input is in the scope of this package. One way you could work around this issue is by preparing the user input before it is passed into this package, either by using a middleware on the server-side or by preparing the input before submitting the request from the frontend:
$preparedInput = preg_replace("/\s*,\s*/", ",", $subject)
$pattern = '/([^,]+)(?:[ ,]*)?/';
preg_match_all($pattern, $subject, $matches);
$matches = array_map('trim', $matches[1]); Hope this helps! |
Beta Was this translation helpful? Give feedback.
-
Thanks for your reply. Make sense to not put sanitization inside this package, however I see this more like a correction, like making lowercase keywords in sql query. Because if value delimiter make multiple conditions, it's unlikely that someone would start or end a search with empty space. Like Laravel have middleware for trim spaces, it make sense to trim space also in this case, since delimiter it's like creating a multiple values of the same input. I hope that I explain well myself. Thanks for sharing solution. The best is second. The first don't solve fancy input with only multiple comma like
This return
So the final code become: if(request()->has('filter.q')) {
$pattern = '/([^,]+)(?:[ ,]*)?/';
preg_match_all($pattern, request()->filter['q'], $matches);
$matches = array_map('trim', $matches[1]);
request()->merge([
'filter' => [
'q' => implode(',', $matches)
]
]);
} I will put this in a middleware, but I think would not be bad to add this in package, only in this case when delimiter are used. A bit of correction is done also for include if I understand correctly digging a bit inside the code. Best |
Beta Was this translation helpful? Give feedback.
-
As explained here https://docs.spatie.be/laravel-query-builder/v2/advanced-usage/multi-value-delimiter/ I am using multiple keywords separated by comma, so there is one input for search where users can add keywords separated by comma.
All works perfectly but if users add with spaces after comma (example
keyword1, keyword2, keyword3
) then query become like:If I change array value delimiter' to comma + space ( ', ' ) then works fine. But if users then search without space after comma (example
keyword1,keyword2,keyword3
) then query become:There is a workaround to solve this without adding extra code to clean filter? Like checking if exist comma + space and then replace with just comma... but seem annoying to maintein.
In a such great package I suppose there is something but can't find anything in docs.
thank You!
Beta Was this translation helpful? Give feedback.
All reactions