I don't have a strong opinion on this but I think the "less work" argument
is weak - it was only a few lines of code - not exactly large/complex
solution :-) Also once you've set it up, if you need to do it again, then
you can re-use it - this solution, you have to bodge it every time.

Also from a maintenance point of view, what happens when you leave and
someone else takes over the code - they're sitting there scratching their
head wondering why the hell theres a getMyFieldAgain() method in the
ActionForm that returns the same as getMyField() or why there are
messagesPresent/NotPresent tags all over the jsp or what is the myFieldAgain
in the validation.xml? OK, its not that big a deal, they'll work it out, but
its more straight forward if all they have to do to understand the myField
validation is look at the form/field in the validation.xml?

In my book, thats not a "simpler" solution.

Niall

----- Original Message ----- 
From: "Berke, Wayne [IT]" <[EMAIL PROTECTED]>
To: "Struts Users Mailing List" <[EMAIL PROTECTED]>
Sent: Thursday, April 22, 2004 4:44 PM
Subject: RE: Validation with 2 masks?


Thanks for the reply.

Actually, we thought of a different workaround, requiring somewhat less
work, and
I welcome your comments on it.

Let's say the field we want to validate is "myField".  The getter in the
ActionForm
is getMyField().  Suppose we simply add another method to the form called
getMyFieldAgain
that simply returns myField?  That way we have two different configurable
bean attributes
that point to the same class field.  Then it's simple to configure each one
separately
with different regexes and different messages.

The only sticking point is if that we only want one error message even if
both validations
fail (usually the more specific failure).  But this can be accomplished
through
a combination of <messagesPresent> and <messagesNotPresent> tags.

Needless to say, this workaround is server-side only.  What do you think?

Wayne

-----Original Message-----
From: Niall Pemberton [mailto:[EMAIL PROTECTED]
Sent: Wednesday, April 21, 2004 5:23 PM
To: Struts Users Mailing List
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]





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

Reply via email to