Prevent CGridView by client condition

Hi anyone,

Can we prevent filter on cgridview based on condition?
For example, when the text in the filter column contains unwanted characters (eg: !@#%), then raise an alert and prevent ajax updates in cgridview

Not sure how you mean, but couldn’t validate the search model, before search, and skip filtering if it fails?

@arsyanmw Did you got the solution?

Use beforeAjaxUpdate + input validation:


$this->widget('zii.widgets.grid.CGridView', array(
    'id' => 'my-grid',
    'dataProvider' => $model->search(),
    'filter' => $model,

    'beforeAjaxUpdate' => "function(id, options){
        var input = $('#MyModel_name').val();

        // allow only letters, numbers and space
        var regex = /^[a-zA-Z0-9 ]*$/;

        if(!regex.test(input)){
            alert('Invalid characters detected!');
            return false; // stops ajax update
        }
    }",
));