Roman
Roman

Reputation: 441

Yii2 remove empty fields from url string

I need your help. I have an Active Form in my Yii2 application and when I submit my form it shows values (no matter empty or not) of every field of the form to the GET-string so it looks like domain.com/index?ItemSearch%5Brooms_arr%5D=&ItemSearch%5Bprice_type%5D=& ItemSearch%5Bprice_type%5D=0&ItemSearch%5Barea_from%5D=&ItemSearch%5Barea_to%5D=&... etc.

I need to have cleaner query string which will contain only non-empty params? For example domain.com/index?rooms_arr=12&price_type=normal.

Please suggest me what is the best way to do this?

Upvotes: 0

Views: 1058

Answers (1)

Volodymyr Sitdikov
Volodymyr Sitdikov

Reputation: 424

This is not the yii2 problem. It is a native html form works like this. If you really do want to exclude all not filled inputs from the query string you could filter all this params via jQuery and set them to disable state, here is the code

$('form').submit(function(e){
    var emptyinputs = $(this).find('input').filter(function(){
        return !$.trim(this.value).length;  // get all empty fields
    }).prop('disabled',true);    
});

Upvotes: 2

Related Questions