You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by joann luo <jo...@gmail.com> on 2012/02/27 23:09:20 UTC

How to correctly use alternate messages in the Struts validation framework?

 How to correctly use alternate messages in the Struts validation framework.
  Based on the following information from Programming Jakarta Struts Chapter
11 The Validator Framework
http://books.google.com/books?id=E874l0jaUGQC&pg=PA260&lpg=PA260&dq=change+the+key+values+in+the+validation-rules.xml+if+you+plan+to+use+alternative+messages&source=bl&ots=t5zzY8fSs4&sig=wEOspEiq8XsU2su4yD1WlCz8H6w&hl=en&sa=X&ei=8_ZLT6TOHoPi0QGB4YiTDg&ved=0CCIQ6AEwAA#v=onepage&q=change%20the%20key%20values%20in%20the%20validation-rules.xml%20if%20you%20plan%20to%20use%20alternative%20messages&f=false






“You should add these to your application’s resource bundle, or change the
key values in the validation-rules.xml if you plan to use alternative
messages.”



I have the following code in validator-rules.xml file.



<validator   name="required"

            classname="StrutsValidators"

               method="validateRequired"

         methodParams="java.lang.Object,

                       org.apache.commons.validator.ValidatorAction,

                       org.apache.commons.validator.Field,

                       org.apache.struts.action.ActionMessages,

                       javax.servlet.http.HttpServletRequest"

*                  msg="errors.required.new"/>*



            <validator   name="mask"

            classname=" StrutsValidators"

               method="validateMask"

         methodParams="java.lang.Object,

                       org.apache.commons.validator.ValidatorAction,

                       org.apache.commons.validator.Field,

                       org.apache.struts.action.ActionMessages,

                       javax.servlet.http.HttpServletRequest"

              depends=""

*                  msg="errors.invalid.new"/>                  *





I added the following string to the properties file.



errors.required.new={0} is required NEW!!!

errors.invalid.new={0} is not valid NEW!!!.





My validation.xml file looks like this:



<field property="attribute(Number)"
depends="required,mask,maxbytelength,nonce">

*                                                <msg name="required"
key="errors.required" /> *

                                                <msg name="maxbytelength"
key="errors.maxlength" />

                                                <var>


<var-name>maxbytelength</var-name>


<var-value>32</var-value>

                                                </var>

                                                <var>


<var-name>form</var-name>


<var-value>Info</var-value>

                                                </var>

*                                                <msg name="mask"
bundle="mytest"key="errors.invalid" /> *

                                                <arg0 key="label.Number" />

                                                <var>


<var-name>mask</var-name>


<var-value>^[0-9\-() ]*$</var-value>

                                                </var>

                                    </field>



I still see the standard error messages such as the ones listed below
displayed on the UI.

errors.required.new={0} is required.

errors.invalid.new={0} is not valid.



Is this a Struts bug?  If not, what is the right way to use alternate
validation message in this case?



Thanks a lot.