Regex filtering on non-required input fields
This is an open discussion with 4 replies, filed under General.
Search
I don't know of a way in the core so I usually customise the data source by adding this check into the grab
function (or execute
in 2.3) and manipulating the $this->dsParamFILTERS
array based on the URL parameters.
Thanks, Nick!
Micheal; have you tried the following regexp:{$url-keyword:.*}
note the * should refer to 0 or more letters - should include empty if you want all entries rather then the ones with an empty $url-keyword
Yep, I tried this as well. The problem is different: A MySQL regex
search won't return anything for NULL fields. So if an unrequired field is empty, your "Symphony search string" must be completely emtpy as well if you want the entry to be returned by the datasource. (Symphony does not pass a search to MySQL if it is empty.)
For reference, here is the datasource variant (placed in the grab
function) — you will need the field ID in this case:
// Custom regex filter // Some fields are not required, so adding 'regepxp:' to the // datasource filter would lead to empty results if we search for // empty strings. This is a workaround. if(!empty($this->dsParamFILTERS['360'])){ $this->dsParamFILTERS['360'] = 'regexp:'.$this->dsParamFILTERS['360']; }
The essence: The string regex:
is only added to the search if the search string it is not empty.
Create an account or sign in to comment.
I tried to create regex filters for a datasource on unrequired fields. If the search string is not set, I want to get all entries.
Short story: It doesn't work.
Why? Adding something like
regexp:{$url-keyword:.}
will always pass a search string to MySQL, even if the$url-keyword
is not set. So in this case your datasource will not return those entries which have no value for the field.The only workaround I found was using
{$url-keyword}
in the filter and manipulating the$_GET
array using an extension, like so:(The extension plugs into the
FrontendPageResolved
delegate, hence I know the page ID.)Is there a more elegant solution? Can you do it without any hacks?