-->
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.  [ 8 posts ] 
Author Message
 Post subject: Use dynamic parameters into a validation error message
PostPosted: Fri Jun 11, 2010 10:44 am 
Newbie

Joined: Fri Jun 11, 2010 10:10 am
Posts: 4
Hi,

I'm a newbie.

When validating a class, how to use values of dynamic parameters into a validation error message?

I explain, I want to take into account parameters which will be included into the text of the message referenced by EXCEPTION_ID into a resource file. What is the best mean to do please?

1) @FilenameUnicity(message = "EXCEPTION_ID", groups = {
Default.class, Specific.class })
So in my resource file, I have EXCEPTION_ID = The file %s is already exist
The %s is the value to replace.

2) MessageInterpolator???

Thanks a lot.


Top
 Profile  
 
 Post subject: Re: Use dynamic parameters into a validation error message
PostPosted: Mon Jun 14, 2010 3:16 am 
Newbie

Joined: Fri Jun 11, 2010 10:10 am
Posts: 4
Hi,

Somebody can help me please?
No response at all, a solution must well exist, no?
Thanks.


Top
 Profile  
 
 Post subject: Re: Use dynamic parameters into a validation error message
PostPosted: Mon Jun 14, 2010 5:37 am 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
I am not 100% sure what you want. You can dynamically change the message template by using ConstraintValidatorContext in isValid of your ConstraintValidator implementation of @FilenameUnicity.

Code:
context.disableDefaultConstraintViolation();
context.buildConstraintViolationWithTemplate(myTemplate);
context.addConstraintViolation();


I guess you want to insert the file name into the message. You could do that before you call buildConstraintViolationWithTemplate.

Per default Bean Validation does not allow to interpolate the actual validated values into the error message.

--Hardy


Top
 Profile  
 
 Post subject: Re: Use dynamic parameters into a validation error message
PostPosted: Mon Jun 14, 2010 6:43 am 
Newbie

Joined: Fri Jun 11, 2010 10:10 am
Posts: 4
Thanks for your response.

So, if I well understand you can't do :

@FilenameUnicity(message = "EXCEPTION_ID {PropertyName} or {value}", groups = {
Default.class, Specific.class })

to take automatically the name or the value of the validated field when the interpolate function is called?

If not, can you better explain your code example please, I don't understand?

Thanks.


Top
 Profile  
 
 Post subject: Re: Use dynamic parameters into a validation error message
PostPosted: Mon Jun 14, 2010 6:49 am 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
That's right, consraint parameters can be interpolated, for example the {min} and {max} of the @Size annotation. The property name and/or the actual property value cannot be interpolated out of the box.
The only way to do something in this direction is to do something with ConstraintValidatorContext. How exactly you go about this depends on your requirements.

--Hardy


Top
 Profile  
 
 Post subject: Re: Use dynamic parameters into a validation error message
PostPosted: Mon Jun 14, 2010 7:22 am 
Newbie

Joined: Fri Jun 11, 2010 10:10 am
Posts: 4
Ok Thanks.

If you want, I can explain:

In the isValid() method, I fill a list corresponding to the parameters to insert into the message such as :

ValidatedObject aValidatedObject = new ValidatedObject(value);
ArrayList messageParameters = new ArrayList<String>();
messageParameters.add(value);
aValidatedObject.addErrorParameters(messageParameters);
ValidationErrorParameters.getInstance().addErrorParameters(
this.annotation, aValidatedObject);

A validateObject to know what my error is related parameter, for example in the case of a list of files.

After into the interpolate method, I do this:
List<String> parameters = ValidationErrorParameters.getInstance().
getErrorParameters(context.getConstraintDescriptor().
getAnnotation(),context.getValidatedValue());

The problem is the validated value returns by context.getValidatedValue() is my validated bean and not the validated property or better the value to compare with my validateObject and to recover the good error message and the linked parameters.

Implementation with ConstraintValidatorContext can do that please?

Thanks.


Top
 Profile  
 
 Post subject: Re: Use dynamic parameters into a validation error message
PostPosted: Mon Jun 14, 2010 8:08 am 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
Hi,

to be honest, I don't understand your code examples. It seems a little out of context. It would help if you posted your ConstraintValidator implementation together with some example entity using the annotation. You could also try to format your examples a little better.

But you are right, I forgot that via MessageInterpolatorContext you have access to the validated value during the message interpolation phase. I guess that's another way of going about this. context.getValidatedValue() should return the validated value, if not there might be a bug.


Top
 Profile  
 
 Post subject: Re: Use dynamic parameters into a validation error message
PostPosted: Mon Jun 14, 2010 8:11 am 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
Created HV-333


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 8 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.