And if you want to do a javascript validation on it as well, don't forget to
set the <var-jstype> attribute to be regexp in the validation.xml. Not that
I struggled with that problem for a week or anything...

Matt


----- Original Message ----- 
From: "Niall Pemberton" <[EMAIL PROTECTED]>
To: "Struts Users Mailing List" <[EMAIL PROTECTED]>
Sent: Wednesday, April 21, 2004 4:23 PM
Subject: Re: Validation with 2 masks?


> Theres nothing in the "standard" mask validation that ships with struts
that
> allows you to do this, but it would be pretty straight forward to write
and
> configure your own validator to do this.  Set up a static mehtod somewhere
> that does pretty much what the FieldChecks.validateMask() method does,
> except modify the "variable" name to something like "mask2"....
>
>     public static boolean validateMask(Object bean,
>                                        ValidatorAction va, Field field,
>                                        ActionMessages errors,
>                                        HttpServletRequest request) {
>
>         String mask = field.getVarValue("mask2");
>         String value = null;
>         if (isString(bean)) {
>             value = (String) bean;
>         } else {
>             value = ValidatorUtils.getValueAsString(bean,
> field.getProperty());
>         }
>
>         try {
>             if (!GenericValidator.isBlankOrNull(value)
>                 && !GenericValidator.matchRegexp(value, mask)) {
>
>                 errors.add(
>                     field.getKey(),
>                     Resources.getActionMessage(request, va, field));
>
>                 return false;
>             } else {
>                 return true;
>             }
>         } catch (Exception e) {
>             log.error(e.getMessage(), e);
>         }
>         return true;
>     }
>
> Then change the validation-rules.xml to add the second mask validator
>
>      <validator name="mask2"
>             classname="myPackage.MyFieldChecks"
>                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.mask2"/>
>
> Then in your validation.xml, use both the standard mask and your mask2
> validator:
>
>    <form name="....">
>         <field property="..." depends="mask, mask">
>               <var>
>
<var-name>mask</var-name><var-value>^.......$</var-value>
>               </var>
>               <var>
>
<var-name>mask2</var-name><var-value>^.......$</var-value>
>               </var>
>         </field>
>
>
>
> ----- Original Message ----- 
> From: "Berke, Wayne [IT]" <[EMAIL PROTECTED]>
> To: "Struts Users Mailing List" <[EMAIL PROTECTED]>
> Sent: Wednesday, April 21, 2004 2:03 PM
> Subject: Validation with 2 masks?
>
>
> I'd like to a property validation against 2 different regular expressions
> with 2 different error messages.
>
> This seems like a fairly common use.  Is there a best practice for it?
>
> Wayne
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
>
>
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
>


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to