Validating double field in struts 2 - validation

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.

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.

try this,
<field name="percentage">
<field-validator type="double">
<param name="minExclusiveExpression">${minExclusiveValue}</param>
<param name="maxExclusiveExpression">${maxExclusiveValue}</param>
<message>Percentage needs to be between ${minExclusive} and ${maxExclusive} (exclusive)</message>
</field-validator>
</field>

Related

Thymeleaf: resolve most specific of multiple message codes

I have an array of possible i18n message codes. Lets call it codes. Let's assume it contains values
[most.specific, specific, default]
Let's assume the messages.properties contains messages for specific and default, but not for most.specific.
I would be happy to be able to write in my Thymeleaf template something like
#{${codes}}
to get a message for specific code (since it is the first one that is defined).
I need Thymeleaf to resolve the first available message code that is defined in messages file into a string, ignoring the other message codes.
How to do this concisely?

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

Difference between an error code and a message code as far as Spring validation is concerned?

I am in reference to the following method from BindingResult:
BindingResult.html#resolveMessageCodes(java.lang.String, java.lang.String)
I am trying to figure out the difference between an error code and a message code. Can someone please provide an example, especially one that would illustrate why there could be several message codes for a given error code?
Because web applications are internationalized, when you reject an object and want to have a message displayed for it, you don't use a hardcoded text because that will show the same no matter the language.
Instead, you specify an error code that later server as a key to retrieving the proper message from the bundles (and now the error code is a message code from the point of view of the method that must find the proper message text).
Your error code resolves to more message codes because Spring (based on the implementation) adds some additional ones for you. Here is a snippet from the Spring documentation:
[...] What error codes it registers is determined by the MessageCodesResolver that is used. By default, the DefaultMessageCodesResolver is used, which for example not only registers a message with the code you gave, but also messages that include the field name you passed to the reject method. So in case you reject a field using rejectValue("age", "too.darn.old"), apart from the too.darn.old code, Spring will also register too.darn.old.age and too.darn.old.age.int (so the first will include the field name and the second will include the type of the field); this is done as a convenience to aid developers in targeting error messages and suchlike. [...]
The last statement is the reason there are more message codes, to have control on the message that is displayed to the user, from a generic one (e.g. "Value required") to a more specific one given the context (e.g. "A value is required for field XXX").
The javadoc for DefaultMessageCodesResolver explains it further and gives an example:
For example, in case of code "typeMismatch", object name "user", field "age":
try "typeMismatch.user.age"
try "typeMismatch.age"
try "typeMismatch.int"
try "typeMismatch"
This resolution algorithm thus can be leveraged for example to show specific messages for binding errors like "required" and "typeMismatch":
at the object + field level ("age" field, but only on "user");
at the field level (all "age" fields, no matter which object name);
or at the general level (all fields, on any object).

Collapsing several "required field missing" errors down to one line in the "stripes:errors" box

I realize this is kind of "Stripes 101", but I guess I skipped that class.
I'd like to use the code that drops the "error" class into the field
tag class strings as the primary way to indicate missing required
fields. I'd then like to show a single error message along with any
other errors (like fields supplied with improper values).
Thus, the error info on the page might look like:
Sorry for the trouble, but:
1. Please supply values for all indicated required fields.
2. Please supply a numeric value for Zip Code.
etc.
I suppose I could go through the validation errors, look for ones for
fields where the value is empty, and make the assumption that those
were the required field errors. However, if I take those errors out
of the set, then I suspect that the page will no longer be able to
mark the erroneously-missing fields with the "error" class.
This seems like something likely to be pretty easy, but I can't find
any specific examples. Maybe I can do it all in the .jsp code; I'll start looking into that.
The DefaultActionBeanPropertyBinder adds an instance of ScopedLocalizableError with the default scope value of "validation.required" when a required field is missing.
So you coult probably write a tag similar to the stripes errors tag, but which collapes all the instances of ScopedLocalizableError having a default scope of "validation.required" to a single error message.

Organize validation messages in Struts2 validation(XML)

I am trying to do validations in struts 2 for my current project. I have to group my validation messages. For Eg: If there are 3 fields that are empty and there are 3 other fields whose format is not right, I need to get a msg like
"The following fields are required: field1, field2, field3
The format of the following fields are invalid: field4, field5, field6"
I tried providing a param to fieldError.
Eg:
< s : fielderror >
< s : param value="%{requiredstring}"/>
< / s : fielderror>
According to me this is like specifying "show all errors whose validator type is requiredstring". Please correct me if I am wrong.
But this will display the message "The following fields are required" each time for every field that is empty. I want it displayed only once.
Is there a way to do this cleanly in stuts2 using validation through xml? I donot want to do all the validations in a validate method.
Thanks
You are wrong; I have no idea why you thought that'd work, the docs don't imply that's possible.
Field errors are just that--errors for a specific field. If you need to group errors by arbitrary criteria, like the validation type, you'll need to implement that yourself.
There are a number of ways to do this, including writing a custom validation interceptor, providing validators that group errors in a different way, or simply gathering the appropriate messages in an action or validation method.
You could gather errors based on the message content, but IMO that would be brittle. If this is a cross-application issue, you're better off doing it a different way.
All that said, by presenting error messages in an order not necessarily reflective of the form, you're pushing more cognitive overhead onto the user: I don't want to see groups of messages telling me which fields share the same error, I want to see what's wrong with each field, in the same order the fields are presented on the form, preferably near the form field itself.

Resources