You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by "Berke, Wayne [IT]" <wa...@citigroup.com> on 2004/04/21 15:03:01 UTC

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: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: Validation with 2 masks?

Posted by Matt Bathje <mp...@ntsource.com>.
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" <ni...@blueyonder.co.uk>
To: "Struts Users Mailing List" <us...@struts.apache.org>
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]" <wa...@citigroup.com>
> To: "Struts Users Mailing List" <us...@struts.apache.org>
> 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: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: Validation with 2 masks?

Posted by Niall Pemberton <ni...@blueyonder.co.uk>.
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]" <wa...@citigroup.com>
To: "Struts Users Mailing List" <us...@struts.apache.org>
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: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org





---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org