-->
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: i18n and cascade validation
PostPosted: Wed Feb 13, 2013 3:26 pm 
Newbie

Joined: Thu Mar 16, 2006 8:46 am
Posts: 3
Hi,

I have this classes:

Code:
class Address {
    @NotEmpty(message = "{address.notEmpy}")
    String body
}

class Contact {
    @Valid
    @NotNull
    Address address
}

class Person {
    @Valid
    @NotNull
    Contact contact

    @Valid
    @NotNull
    Address address
}


Is there a way to obtain different messages when I try to validate a Person with the "address.body" null in both Person and Contact?
Right now I've got:

Quote:
ConstraintViolationImpl{interpolatedMessage='may not be empty', propertyPath=contact.address.body, rootBeanClass=class Person, messageTemplate='{address.notEmpy}'}
ConstraintViolationImpl{interpolatedMessage='may not be empty', propertyPath=address.body, rootBeanClass=class Person, messageTemplate='{address.notEmpy}'}



Thanks!
Mihai


Top
 Profile  
 
 Post subject: Re: i18n and cascade validation
PostPosted: Wed Feb 13, 2013 3:56 pm 
Hibernate Team
Hibernate Team

Joined: Sat Jan 24, 2009 12:46 pm
Posts: 388
Hi,

So you want to retrieve one violation in that case? I think your problem can be used by implementing a custom class-level constraint which you use instead of the @NotNull constraints. You'll find more info in our reference guide:

* class-level constraints: http://docs.jboss.org/hibernate/stable/validator/reference/en-US/html_single/#d0e341
* custom constraints: http://docs.jboss.org/hibernate/stable/validator/reference/en-US/html_single/#validator-customconstraints

Hth,

--Gunnar

_________________
Visit my blog at http://musingsofaprogrammingaddict.blogspot.com/


Top
 Profile  
 
 Post subject: Re: i18n and cascade validation
PostPosted: Wed Feb 13, 2013 5:27 pm 
Newbie

Joined: Thu Mar 16, 2006 8:46 am
Posts: 3
Hi,

Having

Code:
@NotEmpty(message="{notEmpty}")


I need to get 2 different messages:

1. - key: person.address.body.notEmpty
- value: Specify your address.
2. - key: person.contact.address.body.notEmpty
- value: Specify your contact address.

It is possible?

Thanks,
Mihai


Top
 Profile  
 
 Post subject: Re: i18n and cascade validation
PostPosted: Fri Feb 22, 2013 1:47 pm 
Hibernate Team
Hibernate Team

Joined: Sat Jan 24, 2009 12:46 pm
Posts: 388
Hi Mihai,

If the type of an address is known within the Address class, you could do the following:

* Add two @NotNull constraints with different messages and add them to two different validation groups:

Code:
class Address {
    @NotEmpty.List({
        @NotEmpty(message = "{person.address.body.notEmpty}", groups=PersonAddressChecks.class),
        @NotEmpty(message = "{address.notEmpy}", groups=ContactAddressChecks.class)
    })
    String body;
}


* Implement a group sequence provider for the Address class (see 2.3.2.2 in our reference guide - http://docs.jboss.org/hibernate/stable/validator/reference/en-US/html_single/#section-default-group-class). DefaultGroupSequenceProvider is a Hibernate-Validator specific API which allows to determine the applying default group sequence in a dynamic way:

Code:
public class AddressGroupSequenceProvider implements DefaultGroupSequenceProvider<Address> {
    public List<Class<?>> getValidationGroups(Address address) {
        if ( address.getType() == AddressType.PERSON ) ) {
            return Arrays.asList ( PersonAddressChecks.class, Address.class );
        }
        else {
            return Arrays.asList ( ContactAddressChecks.class, Address.class );
        }
    }
}


* Add the provider to your Address class:

Code:
@GroupSequenceProvider(AddressGroupSequenceProvider.class)
class Address {
    //...
}


That way the registered group sequence provider will be used to determine the applying groups when validating the Address within the default group. Depending on the address type, either the PersonAddressChecks group or the ContactAddressChecks group will be validated, causing the @NotEmpty constraint with the right message to fail.

--Gunnar

_________________
Visit my blog at http://musingsofaprogrammingaddict.blogspot.com/


Top
 Profile  
 
 Post subject: Re: i18n and cascade validation
PostPosted: Fri Feb 22, 2013 2:34 pm 
Hibernate Team
Hibernate Team

Joined: Sat Jan 24, 2009 12:46 pm
Posts: 388
Btw. Bean Validation 1.1 (which reached the "proposed final draft" status this week - http://beanvalidation.org/news/2013/02/20/bean-validation-1-1-proposed-final-draft/) will enable a much simpler solution using group conversions (http://beanvalidation.org/1.1/spec/1.1.0.cr1#constraintdeclarationvalidationprocess-groupsequence-groupconversion). Just annotate the different references to Address using the @ConvertGroup annotation:

Code:
class Contact {

    @NotNull
    @Valid
    @ConvertGroup(from=Default.class, to=ContactAddressChecks.class)
    Address address;
}

class Person {

    @NotNull
    @Valid
    Contact contact;

    @NotNull
    @Valid
    @ConvertGroup(from=Default.class, to=PersonAddressChecks.class)
    Address address;
}


During cascaded validation the propagated group (sequence) will be the one configured via @ConvertGroup. As in the previous solution this causes only one of the @NotEmpty constraints to be violated, depending on which path from the root object it was reached. Besides requiring less coding, the big advantage of this approach is that you don't have to know the role of an address within the Address class.

You can try out this approach with the CR1 of Hibernate Validator 5 which we released yesterday (http://in.relation.to/Bloggers/BeanValidationTCK110CR1AndHibernateValidator500CR1Released).

--Gunnar

_________________
Visit my blog at http://musingsofaprogrammingaddict.blogspot.com/


Top
 Profile  
 
 Post subject: Re: i18n and cascade validation
PostPosted: Fri Mar 01, 2013 10:41 am 
Newbie

Joined: Thu Mar 16, 2006 8:46 am
Posts: 3
Thanks, Gunnar!
I will try your recommendation this weekend.


Top
 Profile  
 
 Post subject: Re: i18n and cascade validation
PostPosted: Fri Sep 18, 2015 2:50 pm 
Newbie

Joined: Fri Sep 18, 2015 2:47 pm
Posts: 1
@Gunnar

I implemented the same using GroupSequenceProvider

But the address object in getValidationGroups() is NULL.

Can you tell me where I'm going wrong


Top
 Profile  
 
 Post subject: Re: i18n and cascade validation
PostPosted: Tue Sep 22, 2015 2:54 am 
Hibernate Team
Hibernate Team

Joined: Sat Jan 24, 2009 12:46 pm
Posts: 388
Maybe you validate a Contact or Person instance which has a null Address? Do you have a @NotNull constraint on Contact#address or Person#address to prevent that?

Also I recommend to use @ConvertGroup from Bean Validation 1.1 instead of GroupSequenceProvider if possible (if you are on HV 5.x).

--Gunnar

_________________
Visit my blog at http://musingsofaprogrammingaddict.blogspot.com/


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.