Search code examples
javascriptvalidationdojodojox.grid

Validate that my Dojo Datagrid has at least one row


I have a Dojo form and I am using validation. Everything is working fine for my regular fields and validations. My form contains a DataGrid that is initially empty and the user can add rows. I want my form validation to check that there is at least one row in the table. The validation of fields in the table is working fine but I need to ensure that they have added at least one row.

I tried something like this but it didn't work (ie. the function wasn't called when the rest of the form was validated):

_myGrid.validator = function() {
  debugger;
  console.log("data", _myStore.objectStore.data);
}

Solution

  • I just coded this JSFiddle, I think that it achieves what you want: JSFiddle link

    To start, what you have to do is wrap your grid in a dijit/form/Form. After that, you create a new grid using dojo/_base/declare that extends from both the DataGrid and the _FormMixin. It's on the line 37 in the fiddle.

    The dijit/form/_FormMixin plays an important role here: it will make your grid inherit a few functions, including the validate() and isValid(), which can be overwritten to do your validation. When you call the Form.validate(), the grid will also be validated now. In my example, you can see that whenever the form is submitted and validated, an alert will be displayed with the rows count. This alert was coded in the validate() function from the grid.

    I hope that it helps!