Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Apr 25th, 2012  |  syntax: None  |  size: 2.10 KB  |  hits: 14  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. How to correctly use alternate messages in the Struts validation framework?
  2. <validator   name="required"
  3.                 classname="StrutsValidators"
  4.                    method="validateRequired"
  5.              methodParams="java.lang.Object,
  6.                            org.apache.commons.validator.ValidatorAction,
  7.                            org.apache.commons.validator.Field,
  8.                            org.apache.struts.action.ActionMessages,
  9.                            javax.servlet.http.HttpServletRequest"
  10.                       msg="errors.required.new"/>
  11.  
  12.         <validator   name="mask"
  13.                 classname=" StrutsValidators"
  14.                    method="validateMask"
  15.              methodParams="java.lang.Object,
  16.                            org.apache.commons.validator.ValidatorAction,
  17.                            org.apache.commons.validator.Field,
  18.                            org.apache.struts.action.ActionMessages,
  19.                            javax.servlet.http.HttpServletRequest"
  20.                   depends=""
  21.                       msg="errors.invalid.new"/>                  
  22.  
  23.  
  24. I added the following string to the properties file.
  25.  
  26. errors.required.new={0} is required NEW!!!
  27. errors.invalid.new={0} is not valid NEW!!!.
  28.  
  29.  
  30. My validation.xml file looks like this:
  31.  
  32. <field property="attribute(Number)" depends="required,mask,maxbytelength,nonce">
  33.     <msg name="required" key="errors.required" />
  34.     <msg name="maxbytelength" key="errors.maxlength" />
  35.     <var>
  36.         <var-name>maxbytelength</var-name>
  37.         <var-value>32</var-value>
  38.     </var>
  39.     <var>
  40.         <var-name>form</var-name>
  41.         <var-value>Info</var-value>
  42.     </var>
  43.     <msg name="mask" bundle="mytest"key="errors.invalid" />
  44.     <arg0 key="label.Number" />
  45.     <var>
  46.         <var-name>mask</var-name>
  47.         <var-value>^[0-9-() ]*$</var-value>
  48.     </var>
  49. </field>
  50.  
  51. I still see the standard error messages such as the ones listed below displayed on the UI.
  52. errors.required.new={0} is required.
  53. errors.invalid.new={0} is not valid.
  54.  
  55. Is this a Struts bug?  If not, what is the right way to use alternate validation message in this case?
  56.  
  57. Thanks a lot.