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: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to