Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring "typemismatch" and required fields

In the context of Spring Webflow 2.0.x......

I handle form binding "typemismatches", i.e. as a result of trying to map a String onto a Integer field, by using the following in my messages.properties

typeMismatch={0} contains invalid data.

This works fine.

The problem is that if the field that the typeMismatch error occurred on was "required" then I also receive an error for the missing required field, which is logical I guess because the value that was submitted was never bound. ("Required" being defined in a Commons Validation XML file)

So, I dont want to see the "XXX is required field" error message when the field is only missing due to the typeMismatch. How do I resolve this? I thought about overriding initBinder() on the FormAction but quickly got nowhere.....

like image 203
Lawrence Tierney Avatar asked Sep 02 '10 15:09

Lawrence Tierney


1 Answers

Like Yves mentioned, among the three approaches, i have used a custom validator method and its very easy. You can use a custom validator which checks if the form field already has a xml error message of required. If the field does not have an error, then you can check for your string validation. That way it will display only one.

The other method that you could use is try a multiple xml validation, one being required and the other one being a mask which checks for a particular regular expression. In your case if your field is an integer field, then you can go and perform a mask with regex checking for only numbers. The order of mask, required or required, mask in the xml decides which message gets a higher preference.

For example:

<field property="somefield" depends="required,mask" page="2">
<arg key="somelabel"/>
<var>
    <var-name>mask</var-name>
    <var-value>${somepattern}</var-value>
</var>
</field>
like image 102
Rohit Avatar answered Sep 23 '22 22:09

Rohit