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
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?
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
}
}",
));
Yes, you can prevent the filter request in CGridView by validating the filter input before the AJAX update runs.
A simple way is to check the filter value using JavaScript. If the value contains unwanted characters like !@#% , show an alert and stop the grid from updating.
$this->widget('zii.widgets.grid.CGridView', array(
'id' => 'my-grid',
'dataProvider' => $model->search(),
'filter' => $model,
'beforeAjaxUpdate' => 'function(id, options) {
var isValid = true;
var pattern = /[!@#%]/;
$("#" + id + " .filters input").each(function() {
if (pattern.test($(this).val())) {
alert("Special characters like ! @ # % are not allowed in filters.");
isValid = false;
return false;
}
});
return isValid;
}',
));