Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

remove conversion validation message in struts 2 or make it general

In my struts2 application I have field named carrierNo that accepts integer, when i put string in it gives me this validation error message:

    *Invalid field value for field "carrierNo".*

i can customize this error message in the properties file like this

    invalid.fieldvalue.carrierNo=this field does not accept characters

but i don't want to write a customized message for every non String field in my web application, i want to make it general, i tried the following but it did not work

  invalid.fieldvalue.%{getText(fieldName)}=this field does not accept characters

if there is no way to make general, please help me disable this message at all. then i will use converstion field validator with single message that i define in the properties file.

so my request is to help me make this invalid.fieldvalue.carrierNo general something like this form invalid.fieldvalue.%{getText(fieldName)}

or disable the display of this error message Invalid field value for field "carrierNo".

like image 384
user1512999 Avatar asked Feb 18 '26 14:02

user1512999


1 Answers

You could create your own implementation of ConversionErrorInterceptor which finds out the class of failed field and gets your custom message.

Edit:
See source code for ConversionErrorInterceptor. For example you could do something like this in your custom interceptor inside intercept method

// get field by name from action
Field f = invocation.getAction().getClass().getDeclaredField(propertyName);
// get type of field
Class clz = f.getType();

String message = LocalizedTextUtil.findDefaultText(XWorkMessages.DEFAULT_INVALID_FIELDVALUE + "." + clz,
                    invocationContext.getLocale());

And in your messages.properties file put xwork.default.invalid.fieldvalue.int, xwork.default.invalid.fieldvalue.float, etc.

like image 78
Aleksandr M Avatar answered Feb 21 '26 15:02

Aleksandr M