STRUTS

Page 145

132

Mask Rule

The steps for using the mask rule and defining your own custom message are: 1. Specify that the userName field corresponds to the mask rule: <form name="user"> <field property="userName" depends="required,minlength,maxlength,mask">

This step is similar to the one you performed in the previous example. Now the userName is associated with four rules: required, minlength, maxlength, and mask. If you look up the mask rule in the validator-rules.xml file, you will notice that it is associated with a message called errors.invalid as follows: <validator name="mask" classname="org.apache.struts.validator.FieldChecks" method="validateMask" ‌ depends="" msg="errors.invalid"> ‌

Looking at the default message (errors.invalid) in the resource bundle yields: errors.invalid={0} is invalid.

Look at the above error message, and put yourself in the shoes of the end user. The above is not very specific about why the field is invalid. To be more specific, you need to specify a custom message associated with the mask rule. Just like you can specify custom arguments (arg0, arg1) to messages, you can also specify custom messages. 2. Specify an error message key for the mask rule. Because a generic message for a mask rule would not make sense, you need to specify an error key for this rule using the msg element as follows: <form name="user"> <field property="userName" depends="required,minlength,maxlength,mask"> <msg name="mask" key="userRegistration.userNameMask"/>

The msg element takes two attributes. The name attribute specifies which rule you want to associate the message key with. The key element specifies a key for the message in the resource bundler. Thus, the above message will only be used when there is a mask rule validation error.

Jakarta Struts Live


Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.