validationstruts2strutsstruts-validation

Change Struts Validator default messages


I am new in struts2 . I have created an XML file for validations, but when I test my form I don't get the error messages that I configured in the XML file. instead I get the Struts 2 defaults messages such as this one :

invalid field value for field "capteur.ENERGIE_CAPTEUR".

Is there anyway to make struts2 prints the messages configured in the XML file instead of the default ones ?


Solution

  • That is not a validation error message, it is a conversion error message.

    You can override the default conversion error message up to each single object, by creating an entry for it in the global .properties file, as described in Struts 2 documentation, Type Conversion Errors Handling:

    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.

    If you are interested in understanding how it works in a deeper way, read the Short Story about Validation, Conversion and Friends.