Back to old grids again… I have recently had to use some validation rules in my dojo grid. It took a while to figure out exactly how this is done if one does not use the grid editors, but instead uses the dijit controls. The layout view object contains all of the references needed to put a custom control into your grid.
weatherlayout=[{cells:[[
{name: 'Time', field: "time",
editor:dojox.grid.editors.Dijit,
editorClass:'dijit.form.ValidationTextBox',
editorProps:{regExp:'\\d{4}',invalidMessage:'This number should always be four digits, between 0000 and 2359'}
},
{name: 'Cloud Cover', field: "cloudcover",
editor: dojox.grid.editors.select,
options: ["Clear","Partly Cloudy","Overcast"]
},
{name: 'Wind Direction', field: "winddirection",
editor:dojox.grid.editors.Dijit,
editorClass:'dijit.form.NumberTextBox',
constraint:{min:0,max:359},
editorProps:{required:true}
},
{name: 'Wind Speed', field: "windspeed",
editor:dojox.grid.editors.Dijit,
editorClass:'dijit.form.NumberTextBox',
constraint:{min:0,max:60},
editorProps:{required:true}
},
{name: 'Temp C', field: "temperaturec",
editor:dojox.grid.editors.Dijit,
editorClass:'dijit.form.NumberTextBox',
constraint:{min:0,max:50},
editorProps:{required:true}
},
{name: 'RH%', field: "relativehumidity",
editor:dojox.grid.editors.Dijit,
editorClass:'dijit.form.NumberTextBox',
constraint:{min:0,max:100},
editorProps:{required:true}
}
]]}];
This layout is used in the dojo grid control to define how the grid is displayed. The first column, time, needs a special validation rule. I want to make sure that only 4 dijit numbers are accepted into the control. In order to do this, I first define the fact that I want to use a dijit editor, then I provide a string that identifies the exact dijit class that I want to use. Here is the tricky part… If I want this dijit control to have any custom behavior or initialization settings, I need to specify those using the editorProps object. This editorProps object is used as a mixin later on. Appearantly what happens is that when the grid needs an editor, a new item of the editorClass type is created, and the editorProps are “mixed-in” to form the new object. The approach is pretty slick, but can be a little non-sensical to Object Oriented purists.
One might ask, “What is the difference between editorProps and constraint objects?” The only real difference is that constraint objects get mixed into the normal set of grid editors, and the editorProps get mixed into Dijit controls.