A note about grid validation
I’ve been spending some time looking at the validation techniques used for dojo grid. Basically I’ve come to the conclusion that unless your validation is pretty trivial (drop down lists, etc), the entire thing is still half baked.
What I’ve done instead is adopted the idea that my dojo client code will simply format input data (red=bad, etc) if it needs fixing, and let the user see it. If the user attempts to push bad data, I will catch it on the server and reject it.
That means that all I need to do for dojo is to define a function to be my formatter. This function accepts a single parameter, and returns some html indicating what to display in the grid. So far, so good. Granted, I’d really rather use a robust validation framework for the grid data, but sadly, there isn’t one there.
So when you find yourself hitting 4 hours of frustration on validating grid input, my advice is to format the input, show the user where his error is, and be really good about server side input validation when the data is submitted.
var myformatter = function(s)
{
var RegularExpression = new RegExp("^\\d+$");
if(RegularExpression.test(s))
{
return s;
}
else
{
return "<span style='background-color:red'>"+s+"</span>";
}
Then later on you can use the formatter in your markup like this…
<table dojoType="dojox.grid.DataGrid" store="myStore"
query="{statename: '*' }" style="width:650px;height:500px" >
<thead>
<tr>
<th field=”statename” width=”200px”>State Name</th>
<th field=”population” width=”200px”
editable=”true”
formatter=”myformatter”
>Population</th>
<th field=”spending” width=”200px”
editable=”true” formatter=”mydecimalformatter”
>Spending ($billions)</th>
</tr>
</thead>
</table>
