Extjs 4.1.1 – validatable grid

In this post I will present a simple plugin to Ext.panel.Grid which displays validation errors on a grid. The validation mechanism is based on the model validation – useful feature of Extjs framework. Let’s start from creating a simple model:

As we can see, this model has a validation field where we can define a validation mechanism for every field in our model. In this example the lastname field is required, I use build in Extjs validator of the presence type. More build-in validation types might be found here. Of course, it is possible to define custom validation types, simple example can be found on StackoverflowHaving defined model validation, let’s create ValidatableGridplugin. The main function of ValidatableGrid plugin is function called validate which is attached to grid component. This function looks this way;

This function iterates through every model in grid’s store and runs validation method. As a result, we get a validationResult object which holds information about errors of model. This object has a function isValid which returns true if there is no errors or false otherwise. Having checked the model, we have to show errors on the grid. In order to do that, we have to find a cell, in which invalid field is displayed using getCellByPosition function. In the last step, we only have to modify CSS classes of cell. That is why, I added x-form-invalid-field class to show red line in cell and also data-errorqtip class to show validation error tooltip. The entire listing looks this way;

here is example of usage;

and the result
validatable grid
Source code for this post can be found here

Extjs 4.1.1 – validatable grid

5 thoughts on “Extjs 4.1.1 – validatable grid

  1. TPodolak says: 

    Hi. Entire code listing is presented in the bottom of post. Is there any problem with it?

  2. hi
    Thanks for giving reply

    i want to validate when the button is clicked and for me error msg is not getting displayed in the grid only the red line is getting
    tbar: [{ text: 'submit', handler: function () { if (vGrid.validate() != false) { alert('hai'); } else alert('Fields are required'); } }]
    the above code i have added in the vGrid for button purpose when i click on the button i am getting only red line not the msg and i also want the cell to be editable

  3. hi please help me in this
    i should do validation for editable grid. I use Ext.grid.plugin.CellEditing for edit grid.
    ______________________________________
    |_not_valid_____@|______________|_____|
    |________________|__not_valid__@|_____|
    |_not_valid_____@|______________|_____|

    how can I innsert error message in any cell?

Comments are closed.