You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@struts.apache.org by Roberto Tyley <Ro...@reuters.com> on 2004/02/25 11:23:51 UTC

RE: 1.2.0 uploaded (Re: 1.2.0 is tagged and frozen) - watch out for ActionErrors -> ActionMessages in validation code

I notice that the most recent version of your splendid two-fields
validator (looking in CVS at
http://cvs.sourceforge.net/viewcvs.py/*checkout*/struts/appfuse/src/web/
org/appfuse/webapp/util/ValidationUtil.java) still has the ActionErrors
class in it's method signature:

boolean validateTwoFields(Object bean, ValidatorAction va, Field field,
ActionErrors errors, HttpServletRequest request)

However, you can get some nasty silent errors from struts if you do
this, as the 'errors' variable won't be populated (it'll be null, due to
struts now expecting it to be the ActionMessages class), and when the
null pointer exception occurs in your method (when you attempt to report
a validation error), it will cause some difficult-to-diagnose errors
higher up the stack.

For a bit more insight into the nullness of the 'errors' variable, look
at the initValidator() method in the Resources class:

http://cvs.apache.org/viewcvs.cgi/jakarta-struts/src/share/org/apache/st
ruts/validator/Resources.java

The ActionErrors->ActionMessages change occurred somewhere around rev
1.22 of that class.

The updated version of your validateTwoFields method looks like this:

public static boolean validateTwoFields(
	Object bean,
	ValidatorAction va,
	Field field,
	ActionMessages errors,
	HttpServletRequest request) {
	String value1 = ValidatorUtils.getValueAsString(bean,
field.getProperty());
	String sProperty2 = field.getVarValue("secondProperty");
	String value2 = ValidatorUtils.getValueAsString(bean,
sProperty2);
	try {
		boolean equal = GenericValidator.isBlankOrNull(value1) ?
GenericValidator.isBlankOrNull(value2) : value1.equals(value2);

		if (!equal) {
			errors.add(field.getKey(),
Resources.getActionMessage(request, va, field));
			return false;
		}
	} catch (Exception e) {
		errors.add(field.getKey(),
Resources.getActionMessage(request, va, field));
		return false;
	}

	return true;
}

(I also corrected a small bug at line 44 of the old method where
value1=null, value2='some text' would validate ok!).

Best regards,
Roberto




-----Original Message-----
From: Matt Raible [mailto:lists@raibledesigns.com] 
Sent: 25 February 2004 09:15
To: 'Struts Developers List'
Subject: RE: 1.2.0 uploaded (Re: 1.2.0 is tagged and frozen)

The first thing I noticed is that struts-el is missing from the
download.  I used the one I had from a nightly build in December and it
didn't seem to cause conflicts.

I tried 1.2.0 in AppFuse and all tests pass!  Nice work gents.  I didn't
even have to modify any files - my last Struts update was December 2,
2003.

Matt



--------------------------------------------------------------- -
        Visit our Internet site at http://www.reuters.com

Get closer to the financial markets with Reuters Messaging - for more
information and to register, visit http://www.reuters.com/messaging

Any views expressed in this message are those of  the  individual
sender,  except  where  the sender specifically states them to be
the views of Reuters Ltd.


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