View Full Version : MessageSourceAccessor from a Validator?
tsandor
Nov 19th, 2004, 04:05 AM
Hi!
I'd like to set an error message by error code in my Validator. The problem is that I can't access MessageSourceAccessor easily (now only passing as paramater to an overriden validate method).
Is there any bean or helper class to get or access an ApplicationObjectSupport bean on my current web application context? This way I could set a bean with IoC at least to access my messages that are comming from properties).
Thanks for any help.
Regards,
Thomas
sjivan
Nov 19th, 2004, 08:27 AM
You have access to the Errors object in your implementation of the Validator interface method :
public void validate(Object o, Errors errors)
The org.springframework.validation.Errors interface has several methods that allow you to set the error message by error code. For example
void reject(String errorCode, String defaultMessage);
void rejectValue(String field, String errorCode, String defaultMessage);
Please refer to the javadocs for this class. Also check out the javadoc for the helper class org.springframework.validation.ValidationUtils
Using the methods on the Errors object, you should not require to use MessageSourceAccessor directly to set error messages.
I had a situation where I did need to access the MessageSourceAccessor in my MultiActionController delegate class as I was not working with the Errors class directly. Unlike subclasses of SimplFormController, the delegate class is not required to extend any Spring class and hence does not have access to MessageSourceAccessor. The solution that I came up with was to have my delegate class implement org.springframework.context.ApplicationContextAwar e.
When this is done, the Spring container calls back setApplicationContext(ApplicationContext applicationContext) handing your object an instance of ApplicationContext. Once you have the applicationContext, you can construct a MessageSourceAccessor as follows
MessageSourceAccessor messageSourceAccessor = new MessageSourceAccessor(applicationContext);
Sanjiv
tsandor
Nov 19th, 2004, 08:58 AM
Dear Sanjiv,
Thanks. I'm already using ValidationUtils, and have checked the Errors object you mentioned. MessageResourceAccessor is needed when I'd like to pass a parameter to errorCoded error message as follows:
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "username", "errors.required", new Object[]{msg.getMessage("register.form.username")}, " is required.");
where msg is a parameter of a MessageResourceAccessor.
However your solution on getting an accessor looks great, I will use it if no other way is out to pass a message parameter.
Thomas
tsandor
Nov 24th, 2004, 07:44 AM
Unfortunately MessageResourceAccessor is not request locale-aware by now (SpringWEB v1.1.2) - thanks Chris for the warning. Solution could be that msgSrc.getMessage(String errorCode, Locale locale) should be used. Request locale shoud be accessed, either by RequestContextUtils.getLocale(request) - if we had a request, or by a threadsafe locale.
Colin Sampaleanu had a response to this problem and filed a JIRA at http://opensource.atlassian.com/projects/spring/browse/SPR-485, in connection with another forum thread http://forum.springframework.org/showthread.php?t=10628 that has similar issues.
Thomas
vBulletin® v3.7.3, Copyright ©2000-2008, Jelsoft Enterprises Ltd.