2

My code is following:

var filterParams = {
    'FILTER': null,
    'CQL_FILTER': null,
    'FEATUREID': null
};
if (filter.length > 0) {
    filterParams["CQL_FILTER"] = filter;
    lyr.getSource().updateParams(filterParams);
}
else {
    alert ('Filter is empty')}

When the filter string is invalid, for example, PARAMETER VALUE (without =), lyr.getSource().updateParams(filterParams) silently fails and users don't know that their input is invalid. I'd like to alert the user if the filter is invalid

Open Layers 2 had a means to verify a CQL expression was valid as shown below:

var format = new OpenLayers.Format.CQL();
var filter;
try {
    filter = format.read(cql.value);
} catch (err) {
    output.value = err.message;

Does ol3, ol4, or ol5 have similar functionality?

Even if the expression is valid, lyr.getSource().updateParams(filterParams) silently fails if the fields referenced are incorrect This post describes how to return a list of fields in the layer with WFS or WMS. From that, it would take some work to parse the CQL filter to validate the fields.

Is it asking to much to expect lyr.getSource().updateParams(filterParams) to return an error message so developers don't have to code substantial error checking?

2

Adding lyr.getSource().refresh() will redraw with an empty layer when an invalid cql expression is used.

var filterParams = {
    'FILTER': null,
    'CQL_FILTER': null,
    'FEATUREID': null
};
if (filter.length > 0) {
    filterParams["CQL_FILTER"] = filter;
    lyr.getSource().updateParams(filterParams);
    lyr.getSource().refresh();
}
else {
    alert ('Filter is empty')}
1

You should first see what's in the filter variable , then do a test on it based on what it contains because from what you have said it's length is always > 0 .

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.