Thursday, June 26, 2008

SimpleFormController and Validation

While making your spring application you may come across the need to use a SimpleFormController. If so, there may be questions and confusions. The spring javadoc has some good documentation. Try this cheat sheet too if you are short on time or wish to revise your SimpleFormController knowledge:

SimpleFormController Cheat Sheet

After you get the controller running, you may want to validate your form. Besides using Validator class in the spring framework you may want to explore some configurable validation options. Check springmodules validation:

Spring Modules Validation

This gives you pointers on using Jakarta Commons and valang. I found valang much-much better than using either commons or the spring Validator class for server side validations. For client side validation I still need to make this thing work correctly as my jsp code generates the form on fly using tags. And tags and valang client side thingy dont gel well together. Will post a solution if I find one :).

And remember that expressions given for valang bean are like assertions. So the following check to make sure that a particular field should NOT be null is WRONG

{ myParam : myParam IS NULL : 'This param should not be null.' }

This, in effect says that your param SHOULD be null. So, the correct rule will be:

{ myParam : myParam IS NOT NULL : 'This param should not be null.' }

Your expressions should evaluate to true in order to avoid error. Remember that and you will be happy. Valang allows pretty complex and rigorous test conditions not possible with jakarata commons. Not easily. It is simpler, sweeter and downright good.