-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: Custom Message Interpolator & Inserting values in error mess
PostPosted: Tue Jun 14, 2011 9:28 am 
Newbie

Joined: Mon Mar 07, 2011 9:46 am
Posts: 10
Hi,

I think, I need a custom error message interpolator, because I have to load the error messages from a database and as far as I know, the default message interpolator just supports properties-files. My question is now:
How can I insert values, which are calculated at runtime (here: the names of attributes with an illegal value), into the error message produced by the message interpolator? At the moment, I just see the possibility to place a map with a key identifying the error message and the insertions as values on the bean to be validated by the validator. This map could be filled by my own validator. But then I have the problem, that the validators, which a delivered from hibernate validator (such as @NotNull or @Pattern) will not fill my map (what is not a gread problem...). Is there anywehre a clean possibility to transport such values from the custom validator into the custom message interpolator?

Thanks for your help und kind regards,
Andreas


Top
 Profile  
 
 Post subject: Re: Custom Message Interpolator & Inserting values in error mess
PostPosted: Wed Jun 15, 2011 5:27 pm 
Newbie

Joined: Wed Jun 15, 2011 5:02 pm
Posts: 3
Hi,

Same here:

I extended the MessageInterpolator so that I can use the rootBean, leafBean and the invalidValue in the message templates. Further I added support for nested properties. Here are some samples for (extended) message templates: - "The age of {leafBean.firstname} {leafBean.lastname} must be between {min} and {max} but is {invalidValue}." => "The age of John Doe must be between 18 and 65 but is 17." - "Error on {rootBean}: The the transaction's ({leafBean.transactionId}) amount ({invalidValue}) is too low." - "{invalidValue} is not a valid account number." While this information is unnecessary while you are typing the data and see the whole record, it might be essential if the input is a file and the message is written to a log.

It would also useful to provide some custom properties in the MessageInterpolator context:

beanValidator.validateValue(Person.class, "firstName", person.getFirstName); ... ctx.put("fieldName", "first name"); msgInterpolator.interpolate(constraintViolation.getMessageTemplate(), ctx); - "The field '{context.fieldName}' must not be empty."

While the leafBean is available as "validatedValue" in the MessageInterpolator.Context, I had to extend the context to provide the other values.

The validator has only access to the validated object and the annotation. It is not always possible and reasonable to set the context information on the validated object. It would be good to have some context available in the validator. Now I pass the context information through a threadlocal variable which is very ugly and error prone. E.g. a validator could check if a value is equal to an entry of a dynamic list which depends on the context.

Sometimes it would be useful to pass some context to the validator:
validateBean(Object validationContext, T object, Class<?>... groups)
validateProperty(Object validationContext, T object, String propertyName, Class<?>... groups);
...

public class MyValidator implements javax.validation.ConstraintValidator<MyConstraint, MyObject> {
public boolean isValid(MyObject myObject, ConstraintValidatorContext context) {
((MyService) context.getValidatorContext()).doSomeComplexValidation(myObject);
}

Of course time consuming validation should not be triggered too often. A constaint causing such a validation would typically not define the Default.class group.

See also:
http://relation.to/Bloggers/JSRBeanVali ... mment19490

Regards René

PS: I first tried to post a new topic but then I saw this thread. I hope I'm not spamming, else please just delete the duplicates.


Top
 Profile  
 
 Post subject: Re: Custom Message Interpolator & Inserting values in error mess
PostPosted: Wed Jun 15, 2011 5:30 pm 
Newbie

Joined: Wed Jun 15, 2011 5:02 pm
Posts: 3
Hi,

Same here:

I extended the MessageInterpolator so that I can use the rootBean, leafBean and the invalidValue in the message templates. Further I added support for nested properties. Here are some samples for (extended) message templates: - "The age of {leafBean.firstname} {leafBean.lastname} must be between {min} and {max} but is {invalidValue}." => "The age of John Doe must be between 18 and 65 but is 17." - "Error on {rootBean}: The the transaction's ({leafBean.transactionId}) amount ({invalidValue}) is too low." - "{invalidValue} is not a valid account number." While this information is unnecessary while you are typing the data and see the whole record, it might be essential if the input is a file and the message is written to a log.

It would also useful to provide some custom properties in the MessageInterpolator context:

beanValidator.validateValue(Person.class, "firstName", person.getFirstName); ... ctx.put("fieldName", "first name"); msgInterpolator.interpolate(constraintViolation.getMessageTemplate(), ctx); - "The field '{context.fieldName}' must not be empty."

While the leafBean is available as "validatedValue" in the MessageInterpolator.Context, I had to extend the context to provide the other values.

The validator has only access to the validated object and the annotation. It is not always possible and reasonable to set the context information on the validated object. It would be good to have some context available in the validator. Now I pass the context information through a threadlocal variable which is very ugly and error prone. E.g. a validator could check if a value is equal to an entry of a dynamic list which depends on the context.

Sometimes it would be useful to pass some context to the validator:
validateBean(Object validationContext, T object, Class<?>... groups)
validateProperty(Object validationContext, T object, String propertyName, Class<?>... groups);
...

public class MyValidator implements javax.validation.ConstraintValidator<MyConstraint, MyObject> {
public boolean isValid(MyObject myObject, ConstraintValidatorContext context) {
((MyService) context.getValidatorContext()).doSomeComplexValidation(myObject);
}

Of course time consuming validation should not be triggered too often. A constaint causing such a validation would typically not define the Default.class group.

See also:
http://relation.to/Bloggers/JSRBeanVali ... mment19490

Regards René

PS: I first tried to post a new topic but then I saw this thread. I hope I'm not spamming, else please just delete the duplicates.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.