Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Validating double field in struts 2

I have a field "length" in one of my struts 2 form. The data-type of "length" is "double". I have applied the "double" validation in XML file. But when I key-in alphabets in the "length" text field, it shows the error message as

Invalid field value for field "length"

I don't want this message to be shown like this. This message is generated by struts 2 itself and not entered by me. I guess, this message comes as the conversion of data fails. I also applied the "conversion" validator, but the above error message is still showing up. Please suggest the solution.

Thanks in advance.

like image 554
Saurabh Avatar asked Sep 09 '26 07:09

Saurabh


1 Answers

You're in luck. This text is customizable.

The text is defined in xwork-messages.properties in the xwork jar. You can override it by adding the following to your global i18n resource bundle:

xwork.default.invalid.fieldvalue=Invalid field value for field "{0}".

As you guessed, this error message occurs for all type conversion failures. The XWorkConverter class has some useful javadoc about this:

Any error that occurs during type conversion may or may not wish to be reported. For example, reporting that the input "abc" could not be converted to a number might be important. On the other hand, reporting that an empty string, "", cannot be converted to a number might not be important - especially in a web environment where it is hard to distinguish between a user not entering a value vs. entering a blank value.

By default, all conversion errors are reported using the generic i18n key xwork.default.invalid.fieldvalue, which you can override (the default text is Invalid field value for field "xxx", where xxx is the field name) in your global i18n resource bundle.

However, sometimes you may wish to override this message on a per-field basis. You can do this by adding an i18n key associated with just your action (Action.properties) using the pattern invalid.fieldvalue.xxx, where xxx is the field name.

It is important to know that none of these errors are actually reported directly. Rather, they are added to a map called conversionErrors in the ActionContext. There are several ways this map can then be accessed and the errors can be reported accordingly.

like image 173
Steven Benitez Avatar answered Sep 12 '26 22:09

Steven Benitez