Validator uses
Struts' Resource Bundle mechanism for externalizing error messages. Instead of having hard-coded error messages in the framework, Validator allows you to specify a key to a message in the ApplicationResources.properties file that should be returned if a validation fails. Each validation routine in the validator-rules.xml file specifies an error message key with the validator tag's msg attribute, as follows:
<validator name="required"
classname="org.apache
.struts.validator.FieldChecks"
method="validateRequired"
methodParams="java.lang
.Object, org.apache.commons.validator
.ValidatorAction, org.apache.commons
.validator.Field, org.apache.struts
.action.ActionErrors, javax.servlet
.http.HttpServletRequest"
msg="errors.required">
If the validation fails when it is run, the message corresponding to the key specified by the msg attribute will be returned.
The following snippet shows the default set of validation error messages from the ApplicationResources.properties file that comes prepackaged with Struts example applications. Each message key corresponds to those specified by the validation routines in the validator-rules.xml file, which also comes prepackaged with Struts example applications.
# Error messages for Validator framework validations
errors.required={0} is required.
errors.minlength={0} cannot be less than {1} characters.
errors.maxlength={0} cannot be greater than {2} characters.
errors.invalid={0} is invalid.
errors.byte={0} must be a byte.
errors.short={0} must be a short.
errors.integer={0} must be an integer.
errors.long={0} must be a long.0. errors.float={0} must be a float.
errors.double={0} must be a double.
errors.date={0} is not a date.
errors.range={0} is not in the range {1} through {2}.
errors.creditcard={0} is not a valid credit card number.
errors.email={0} is an invalid e-mail address.
Note that each message has placeholders, in the form {0}, {1}, or {2}. At runtime the placeholders are replaced by another value such as the name of the field being validated. This feature is especially useful in allowing you to create generic validation error messages that can be reused for several different fields.
Take, for example, the required validation's error message, errors.required, shown here:
errors.required={0} is required.
When you use the required validation in the validation.xml file, you have to define the value that should be used to replace {0} in the error message, as shown here.
<form name="auctionForm">
<field property="bid" depends="required">
<arg0 key="prompt.bid"/>
</field>
</form>
Error messages can have up to four placeholders: {0} through {3}. These placeholders are known as arg0 through arg3, respectively, and you can specify them by using the arg0 - arg3 tags. In the example above, the arg0 tag specifies the value that should be used to replace the {0} placeholder. This tag's key attribute specifies a message key from the ApplicationResources.properties file, such as the following, whose value is used as the replacement for the placeholder:
prompt.bid=Auction Bid
Using a message key for the placeholder value frees you from having to hard-code the replacement value over and over in the validation.xml file. However, if you don't want to use the Resource Bundle key/value mechanism for specifying placeholder values, you can explicitly specify the placeholder value by using the following syntax for the arg0 tag.
<arg0 key="Auction Bid" resource="false"/>
In this example, the resource attribute is set to false to instruct Validator that the value specified with the key attribute should be taken as the literal placeholder value and not as a key for a message in the ApplicationResources.properties file.