struts-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From christop...@marsh-bourdon.com
Subject ActionErrors and the example code
Date Thu, 20 May 2004 09:05:13 GMT
I have developed my way around this issue, but I wanted to pass it by you to see if my logic
is not flawed.

In the example given with the Struts packages, it performs validation within a form like so:

public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) 
{
   // Perform validator framework validations
   ActionErrors errors = super.validate(mapping, request);

   // Only need crossfield validations here
   if (!password.equals(password2)) 
   {
   	errors.add("password2",
                       new ActionError("error.password.match"));
   }
   return errors;
}

I believe that an error will be raised at the line beginning errors.add(.  The reason is the
super.validate will return a null if no errors are created within it, and you can’t perform
an ‘add’ on a null object.  Therefore, the code should look like this:

public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) 
{
   // Perform validator framework validations
   ActionErrors errors = super.validate(mapping, request);

   // Only need crossfield validations here
   if (!password.equals(password2)) 
   {
   	if (errors == null)
	    errors = new ActionError();

   	errors.add("password2",
                       new ActionError("error.password.match"));
   }
   return errors;
}

Am I correct in my logic?  I have tested this and I find it to be true.

Christopher


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


Mime
View raw message