-->
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.  [ 3 posts ] 
Author Message
 Post subject: Validation when the setter is invoked?
PostPosted: Tue Jan 29, 2008 7:58 am 
Newbie

Joined: Wed Jan 16, 2008 8:23 am
Posts: 7
Hi,

I want to apply validation rules to our business objects, and since it makes sense to report problems to the caller as soon as possible (instead of waiting until they have assembled the whole object), I would like to invoke each single-property validator (e.g. @NotEmpty) when the setter of that property is called.

I don't want an AOP proxy just for this, I'm OK with triggering the validation manually from the setter, but I'd to use Hibernate Validator's annotations.

Examples:

Code:
// Version 1: looks nice, but the validation doesn't get invoked when the setter is called.
@NotEmpty
public String getName() {
    return name;
}

public String setName(String name) {
    this.name = name;
}


Code:
// Version 2: the validation takes place instantly, but it doesn't use the Hibernate Validator framework.
public String getName() {
    return name;
}

public String setName(String name) {
    if (name == null || name.length() == 0) {
        // report problem
    }
    this.name = name;
}


Code:
// Version 3: I need something like this
@NotNull
public String getName() {
    return name;
}

public String setName(String name) {
    // Run validation based on object type, property name & property value
    ValidatorManager.validate(this.getClass(), "name", name);
    this.name = name;
}


How can I do this?

Thanks,
Peter


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 29, 2008 3:43 pm 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
I think you can achieve your version three by using Application-level validation. Check the Hibernate Validator documentation for it.

http://www.hibernate.org/hib_docs/validator/reference/en/html_single/#d0e436


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 31, 2008 1:01 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
I don't like the idea of polluting the domain model with the validator code.

I'd rather use
Code:
ClassValidator addressValidator = new ClassValidator( Address.class, ResourceBundle.getBundle("messages", Locale.ENGLISH) );

//only get city property invalid values
InvalidValue[] validationMessages = addressValidator.getInvalidValues(address, "city");

//only get potential city property invalid values
InvalidValue[] validationMessages = addressValidator.getPotentialInvalidValues("city", "Paris")


when the client is called. That is what Seam is doing.

_________________
Emmanuel


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