1

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);
}
Gary Joy
  • 103
  • 8
  • Have you tried creating a validate() function instead of validator? – pgianna May 31 '17 at 09:13
  • I tried adding .validate() and .isValid() (to the DataGrid widget) but nothing happens (i.e. my code is not executed when the Form is validated). Is that what you meant? – Gary Joy May 31 '17 at 15:15

3 Answers3

1

did you try something like this?

if(this.yourformname.validate()&& this.grid.get('rowCount')>0){
//do your stuff
}else{
alert("either form is missing data or grid has no rows");
}
bajji
  • 1,271
  • 3
  • 15
  • 37
  • I'm trying to avoid this approach (particularly the notification aspect i.e. I want validation issues conveyed in a consistent manner - which dojo does with red colouring and tooltips) but I'm going to call this Plan B. If I don't get (or come up with) anything better in the next day or two I will accept this answer. – Gary Joy May 31 '17 at 15:22
  • Why don't you put a border (red color) across grid in this scenario? This way user knows that grid is blank. or you can change the 'noDataMessage' attribute with css style – bajji Jun 01 '17 at 19:29
1

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!

Community
  • 1
  • 1
  • Your JSFiddle does achieve what I want. Thanks! It's a shame that I will have to sort out the notification part (i.e. how to tell the user that they need to add a row) myself but I can deal with that. I had some problems applying your approach because my form wasn't being created in code (it wasn't happy that the `DataGrid` didn't have a `containerNode`) but I did some rework and everything is working now. – Gary Joy Jun 01 '17 at 09:07
0

Well, Below is the way to check the data grid row count.

Note- I am not aware of the dojo version you are using in project however dojo 1.6 provides direct property for row count i.e. dataGridObject.rowCount

Working code-

JSFiddle- http://jsfiddle.net/vikash2402/NNQpn/162/

Hoping this will help you :)

Vikash Pandey
  • 5,407
  • 6
  • 41
  • 42
  • I'm using dojo 1.10. I know how to check the row count, my question is how I can integrate my check of the row count into the standard pattern for validation in dojo i.e. when I call form.validate() I want the DataGrid to complain if there are no rows (just as my other Widgets complain if they are mandatory and no data has been entered). – Gary Joy May 31 '17 at 15:18