11

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.....

Stu Thompson
  • 38,370
  • 19
  • 110
  • 156
Lawrence Tierney
  • 856
  • 1
  • 12
  • 30

2 Answers2

2

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>
Rohit
  • 523
  • 2
  • 6
  • 25
1

You have many options, in order of preference:

  • Set selectively the message typeMismatch.target.yourFieldName or typeMismatch.int in resources files

  • Implement your own Validator so that you can send a dedicated message when Integer parsing will fail before the binding step

  • Create a BindingErrorProcessor to handle different kind of parsing issues

Community
  • 1
  • 1
Yves Martin
  • 10,217
  • 2
  • 38
  • 77
  • A normal chain configuration is validator first and then binding. Maybe yours is switched. – Yves Martin Mar 13 '12 at 10:42
  • Validators used the "required" resource for their message – Yves Martin Mar 13 '12 at 10:44
  • So, I have @Required on top of a field and get then I get that typeMismatch validation error. How I do that only the message about typeMismatch error is shown and required error message not? – mico Mar 13 '12 at 10:48
  • So, I have a variable at persistence class where I have annotation @NotNull on top of Integer xy. I give a string, let's say "foo" and try to save. I get two errors: one about typeMismatch and another of field is required. Exactly like in the main question. – mico Mar 13 '12 at 14:50
  • The question is about a @ Required annotation for Commons Validator, not @ NotNull for Hibernate... – Yves Martin Mar 13 '12 at 15:01