-->
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.  [ 2 posts ] 
Author Message
 Post subject: Validation of different class objects from the same class
PostPosted: Tue Oct 30, 2012 5:22 am 
Newbie

Joined: Tue Oct 30, 2012 4:42 am
Posts: 1
Hi everybody,

First of all, I am sorry if my description sounds a bit dumb but I am very new to the field.

The structure of my problem looks like this:

I have two classes which I am using to generate my class objects, FormInfo and People. In FormInfo I create two objects of the class People, Manager and NonManager, which have different validation needs. For the Manager object fields all informations needs to be supplied, so all the fields (firstName, sureName and so on) need to be notBlank. But giving these infos on the NonManager object is optional therefore can be blank.

I have tried to use inner object constraints as follows

Code:
    <bean class="FromInfo" ignore-annotations="false">
        <field name="Manager">
                 <valid/>
        </field>
    </bean>

    <bean class="Manager" ignore-annotations="false">
         <field name="firstName">
             <constraint annotation="org.hibernate.validator.constraints.NotBlank">
            <message>{NotBlank}</message>
      </constraint>
   </field>
    </bean>



so that the firstName of a Manager would be required but the a NonManager would not need to enter his/her name. But it always throughs a javax.validation.ValidationException: Unable to load class: Manager

I am pretty sure I am doing it wrong but I don't understand what I am doing wrong.

Any help is greatly appreciated!!!

Thanks a lot in advance and best regards!

norbert


Top
 Profile  
 
 Post subject: Re: Validation of different class objects from the same class
PostPosted: Fri Nov 02, 2012 6:53 am 
Hibernate Team
Hibernate Team

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

The exception is raised since there is no class named Manager, only FormInfo and People.

Your problem could be solved using validation groups (see http://docs.jboss.org/hibernate/stable/validator/reference/en-US/html/validator-usingvalidator.html#validator-usingvalidator-validationgroups), e.g. like this:

Code:
public interface ManagerChecks {}

public class People {

    @NotBlank(groups=ManagerChecks.class)
    private String firstName;
}

...

//check the constraints of the ManagerChecks group
Validator validator = Validation.buildDefaultValidatorFactory().getValidator();
People manager = createManager();
validator.validate(manager, ManagerChecks.class);


--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.  [ 2 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.