How to require a Filter Parameter in GET


#1

We would like to make it a requirement for people to put value in the Filter field but when we define ‘is not null’ in the service access, it doesn’t work. We would like to retrieve records from Mysql DB based on the filters. Can someone help with this please.

Thanks.


#2

The filters defined in role service access are server side filters that further limit what records users have access to. Don’t think that’s what you need here. Sounds like what you want is to require each GET request to specify a filter as a query parameter.

In that case I would use a server side script for db.tablename.get.pre_process. The script could be very simple like this or you can expand it to further validate the filter string.

if (!event.request.query.filter) {
    throw("Missing filter");
}

Here is a link to wiki and blog posts on server side scripting. The second blog posts shows how to access log files for debugging scripts.

https://github.com/dreamfactorysoftware/dsp-core/wiki/Server-Side-Scripting


#3

Hey, I tried it and variations looking at the documentation and the samples but it keeps giving fatal error i.e. a huge amount of code appears when a get request is given. Can you please give me an example if lets say I want a filter for a field named XYZ i.e., if a get is executed for a particular table, a filter for XYZ must be provided.

Thanks for the help.


#4

One way is to check for filter starts with XYZ.

    var filter = event.request.query.filter;
    if (!filter) {
        throw("Missing filter");
    } else if (filter.indexOf("XYZ") != 0) {
        throw("Bad filter");
    }
    // good filter

If you have problems with scripting try putting a throw(“testing”) at the top of your script. If the script runs properly you should get that error back at the client. Just to make sure you are editing the right script and it is running when expected, then remove the throw().


#5

I am sorry but still giving exceptions. I must be doing something wrong. Also, I meant XYZ is the column name in MYSQL and I wanted to make it compulsory to provide a filter for XYZ column.

Thanks.


#6

We need to verify that the script engine is working properly. That’s why I wanted you to add the throw(“testing”) to your script.

In the root directory for your DSP set DSP_DEBUG to true in web/index.php then restart apache. Also watch the log file in /log. What OS is your DSP running on?