You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@struts.apache.org by Matt Raible <ma...@raibledesigns.com> on 2004/03/22 17:36:07 UTC

Validating Two Fields - how to do in 1.2?

My popular validate-two-fields howto seems to be broke with the Struts
nightly (20031202) build I'm using.  The following (which works w/
Struts 1.1) throws a NPE at the first errors.add():

    public static boolean validateTwoFields(Object bean, ValidatorAction
va,
                                            Field field, ActionErrors
errors,
                                            HttpServletRequest request)
{
        String value =
            ValidatorUtils.getValueAsString(bean, field.getProperty());
        String sProperty2 = field.getVarValue("secondProperty");
        String value2 = ValidatorUtils.getValueAsString(bean,
sProperty2);

        if (!GenericValidator.isBlankOrNull(value)) {
            try {
                if (!value.equals(value2)) {
                    errors.add(field.getKey(), // NPE THROWN HERE
                               Resources.getActionError(request, va,
field));

                    return false;
                }
            } catch (Exception e) {
                errors.add(field.getKey(),
                           Resources.getActionError(request, va,
field));

                return false;
            }
        }

        return true;
    }

I tried adding if (errors == null) errors = new ActionErrors();, then
everything works fine, but getActionError is deprecated.  So how do I
upgrade this to 1.2?

Looking at the example (code below) in the docs
(http://jakarta.apache.org/struts/userGuide/dev_validator.html), there
signature of this method has changed slightly, adding "ServletContext
application".  Also, the Resources.getActionError isn't valid and and
errors.add(String, ActionError) is deprecated.

Any ideas?  I'll try upgrading to a newer build - but I wanted to get
this archived since folks will experience it migrating from 1.1 to 1.2.

Matt


public static boolean validateTwoFields(
    Object bean,
    ValidatorAction va, 
    Field field,
    ActionErrors errors,
    HttpServletRequest request, 
    ServletContext application) {

    String value = ValidatorUtils.getValueAsString(
        bean, 
        field.getProperty());
    String sProperty2 = field.getVarValue("secondProperty");
    String value2 = ValidatorUtils.getValueAsString(
        bean, 
        sProperty2);

    if (!GenericValidator.isBlankOrNull(value)) {
       try {
          if (!value.equals(value2)) {
             errors.add(field.getKey(),
                Resources.getActionError(
                    application,
                    request,
                    va,
                    field));

             return false;
          }
       } catch (Exception e) {
             errors.add(field.getKey(),
                Resources.getActionError(
                    application,
                    request,
                    va,
                    field));
             return false;
       }
    }

    return true;
}




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


RE: Validating Two Fields - how to do in 1.2?

Posted by Matt Raible <li...@raibledesigns.com>.
Nevermind - changing everything to ActionMessages solved it.  Should've
known.  Below are the files I had to change - hopefully this helps folks
in the future.  I'll update my site when 1.2 is released.  When's that
going to happen - August? ;-)

http://tinyurl.com/3x4nw
http://tinyurl.com/2rm65

Matt

> -----Original Message-----
> From: Matt Raible [mailto:matt@raibledesigns.com] 
> Sent: Monday, March 22, 2004 9:36 AM
> To: struts-dev@jakarta.apache.org
> Subject: Validating Two Fields - how to do in 1.2?
> 
> 
> My popular validate-two-fields howto seems to be broke with 
> the Struts nightly (20031202) build I'm using.  The following 
> (which works w/ Struts 1.1) throws a NPE at the first errors.add():
> 
>     public static boolean validateTwoFields(Object bean, 
> ValidatorAction va,
>                                             Field field, 
> ActionErrors errors,
>                                             
> HttpServletRequest request) {
>         String value =
>             ValidatorUtils.getValueAsString(bean, 
> field.getProperty());
>         String sProperty2 = field.getVarValue("secondProperty");
>         String value2 = ValidatorUtils.getValueAsString(bean,
> sProperty2);
> 
>         if (!GenericValidator.isBlankOrNull(value)) {
>             try {
>                 if (!value.equals(value2)) {
>                     errors.add(field.getKey(), // NPE THROWN HERE
>                                
> Resources.getActionError(request, va, field));
> 
>                     return false;
>                 }
>             } catch (Exception e) {
>                 errors.add(field.getKey(),
>                            Resources.getActionError(request, 
> va, field));
> 
>                 return false;
>             }
>         }
> 
>         return true;
>     }
> 
> I tried adding if (errors == null) errors = new 
> ActionErrors();, then everything works fine, but 
> getActionError is deprecated.  So how do I upgrade this to 1.2?
> 
> Looking at the example (code below) in the docs 
> (http://jakarta.apache.org/struts/userGuide/dev_validator.html
> ), there signature of this method has changed slightly, 
> adding "ServletContext application".  Also, the 
> Resources.getActionError isn't valid and and 
> errors.add(String, ActionError) is deprecated.
> 
> Any ideas?  I'll try upgrading to a newer build - but I 
> wanted to get this archived since folks will experience it 
> migrating from 1.1 to 1.2.
> 
> Matt
> 
> 
> public static boolean validateTwoFields(
>     Object bean,
>     ValidatorAction va, 
>     Field field,
>     ActionErrors errors,
>     HttpServletRequest request, 
>     ServletContext application) {
> 
>     String value = ValidatorUtils.getValueAsString(
>         bean, 
>         field.getProperty());
>     String sProperty2 = field.getVarValue("secondProperty");
>     String value2 = ValidatorUtils.getValueAsString(
>         bean, 
>         sProperty2);
> 
>     if (!GenericValidator.isBlankOrNull(value)) {
>        try {
>           if (!value.equals(value2)) {
>              errors.add(field.getKey(),
>                 Resources.getActionError(
>                     application,
>                     request,
>                     va,
>                     field));
> 
>              return false;
>           }
>        } catch (Exception e) {
>              errors.add(field.getKey(),
>                 Resources.getActionError(
>                     application,
>                     request,
>                     va,
>                     field));
>              return false;
>        }
>     }
> 
>     return true;
> }
> 
> 
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: struts-dev-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: struts-dev-help@jakarta.apache.org
> 



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