bval-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Matt Benson <gudnabr...@gmail.com>
Subject Re: determine between class and property level constraint
Date Thu, 10 Oct 2013 15:48:01 GMT
I am still not sure of your terminology.  If by "class level" you mean the
constraint in question was declared on the class of the root bean being
validated, then yes.  Actually, no--the right test would be
violation.getPropertyPath().iterator().next().getName() == null.  If by
"class level" you mean to learn whether the constraint was declared at the
class level anywhere in your object graph, this test is not sufficient.

The most efficient way I know to get an EL-compatible path such as you
describe, at least in Apache BVal, is to invoke
violation.getPropertyPath().toString().  ;)

Matt


On Thu, Oct 10, 2013 at 10:33 AM, Umesh Awasthi <umeshawasthi@gmail.com>wrote:

> Thanks Maat for your inputs,in short you mean if
> "violation.getPropertyPath().
> 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.firstName
> 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?
>
> Thanks
> Umesh
>
>
> On Thu, Oct 10, 2013 at 8:51 PM, Matt Benson <gudnabrsam@gmail.com> 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 <umeshawasthi@gmail.com>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()){
>>>             leafNode=violationNodes.next();
>>>         }
>>>
>>>         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
>>> http://www.travellingrants.com/
>>>
>>>
>>>
>>
>>
>
>
> --
> With Regards
> Umesh Awasthi
> http://www.travellingrants.com/
>
>
>

Mime
View raw message