bval-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Umesh Awasthi <>
Subject Re: determine between class and property level constraint
Date Thu, 10 Oct 2013 15:33:19 GMT
Thanks Maat for your inputs,in short you mean if
iterator().hasNext()." have elements than constraints is property level
else its a class level?

Additionally i have one more issue to deal for nested bean properties
validation, say i have one bean "User" with few properties and another bean
"User Credentials" with more properties, so in order to populate data
Underlying framework uses following naming mechanism for HTML input fields

user.user Credential.username etc

which will be converted to getUser().setFirstName() etc by framework to
fill data

and in order to show these messages at UI level, i need to get information
about field name being validated, one option is iterate through
"ConstraintViolation.getPropertyPath().iterator()" and build field names
but i am wondering if there is any other more efficient way to do this?


On Thu, Oct 10, 2013 at 8:51 PM, Matt Benson <> wrote:

> Hello,
> Your sample code seems to do more work than necessary for the result it
> finds.  Every Node returned by the Path should be non-null, therefore your
> whole test could be reduced to
> violation.getPropertyPath().iterator().hasNext().
> I'm not sure the above actually gives you exactly what it is you're
> looking for.  If you simply want to know whether the violated constraint
> was declared as a class level constraint of the root bean, then it works.
>  But to use the Business/Employee model as an example, if you need to
> differentiate between a constraint declared on the Employee class as
> opposed to one declared on Business.manager (of type Employee), your
> approach will fall short.
> It appears to me as though (violation.getLeafBean() ==
> violation.getInvalidValue()) should be true when the constraint was
> declared on the bean class.
> Matt
> On Thu, Oct 10, 2013 at 4:44 AM, Umesh Awasthi <>wrote:
>> I need to determine type of constraint violation from
>> [ConstraintViolation][1] object.
>> one way is to use `ConstraintViolation#getPropertyPath()`. If `getName()`
>> returns null on the `leaf` node you have a class level constraint,
>> otherwise a property level constraint.
>> One option is like
>>      Iterator<Node> violationNodes=violation.getPropertyPath().iterator();
>>         Node leafNode=null;
>>         while (violationNodes.hasNext()){
>>   ;
>>         }
>>         if(leafNode!=null){
>>         // property constraint
>>         }
>>         else{
>>          // class constraint
>>         }
>> Is this good approach to determine or there can be other efficient or
>> good approach to do this?
>> --
>> With Regards
>> Umesh Awasthi

With Regards
Umesh Awasthi

View raw message