• Post Reply Bookmark Topic Watch Topic
  • New Topic

What is the propertyNode of ConstraintValidatorContext?  RSS feed

 
Ranch Hand
Posts: 37
Firefox Browser IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I have this code:


According to the api the addPropertyNode method “Adds a property node to the path the ConstraintViolation will be associated to.
name describes a single property. In particular, dot (.) is not allowed.”

But whatever this value is, it still reads the my.custom.message from ViolationMessages.properties. So, what does this do? When should I use it?

Thanks in advance.
 
Sheriff
Posts: 21378
92
Chrome Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A constraint violation has a message (and template), but also a path to the property that did not pass validation.

In your example, you specify the message template, which leads to the message being loaded from ViolationMessages.properties. The property node takes the current property path (for the root object that's empty), and appends something to it. Those are two different attributes of the constraint violation.
 
Christian Wansart
Ranch Hand
Posts: 37
Firefox Browser IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay, but where does the property path come into account?

It doesn't matter if I add any propertyNode, in this example it will still read the message from the ViolationMessages.property with the property name "my.custom.message".

I don't understand where the propertyNode does anything other than set a value. Where is it used?
 
Rob Spoor
Sheriff
Posts: 21378
92
Chrome Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can use it to report where in a request a constraint violation occurred. If you have one class with four String properties all annotated with @NotEmpty, and one of them is null, it's the property path that you can use which of the four properties is the actual empty one.
 
Everybody's invited. Even this tiny ad:
Why should you try IntelliJ IDEA ?
https://coderanch.com/wiki/696337/IntelliJ-IDEA
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!