-->
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.  [ 5 posts ] 
Author Message
 Post subject: Dynamically selecting fields which fields to validate
PostPosted: Tue Jul 08, 2014 12:12 pm 
Newbie

Joined: Tue Jul 08, 2014 12:04 pm
Posts: 3
Hello all,

I currently have a form bean with attributes that I are annotated with validation annotations and I can successfully validate
form submissions. However I have a requirement to be able to toggle validation on or off for any of the form fields dynamically.
I am looking for an nice way to do this but it seems that the validation groups approach is not applicable, that those groups would
need to be defined at compile time. I need some type of dynamic approach where I can perhaps pass a list of the attributes of the
form that I want validated according to the validation annotations in the form bean class.

Any help is very much appreciated.

Regards


Top
 Profile  
 
 Post subject: Re: Dynamically selecting fields which fields to validate
PostPosted: Wed Jul 09, 2014 9:53 am 
Hibernate Team
Hibernate Team

Joined: Sat Jan 24, 2009 12:46 pm
Posts: 388
You could implement a custom default group sequence provider (see https://docs.jboss.org/hibernate/stable/validator/reference/en-US/html_single/#section-default-group-class) which determines the groups to validate based on the state of your entity.

Hth,

--Gunnar

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


Top
 Profile  
 
 Post subject: Re: Dynamically selecting fields which fields to validate
PostPosted: Thu Jul 10, 2014 5:13 am 
Newbie

Joined: Tue Jul 08, 2014 12:04 pm
Posts: 3
Thank you Gunnar,
Very much appreciated. I have had a look into this approach but unfortunately I think that the GroupSequenceProvider won't be suitable for what I need. As every field can change and the form has many fields that may not be grouped. I would need to provide a GroupSequence for every field in the form. There is also a requirement for a content editor to be able to dynamically update form fields and associate validation constraints per field so it maybe the case that hibernate validation is not a good fit for these requirements.

I think that I need to somehow do the following :

* For each field in a form enable a a CMS editor be able to associate constraints for example
Field A - constraint 1 .. n (name and constraint stored in CMS)

* On form submission - Form processor gets all parameter names from the request and looks up the associated constraints to create a dynamic constraint map {(field a, (constraint1, constraint 2)), (field b, (constraint 1, constraint 3 ) ...}

* Form parameters are bound to a bean which is then validated against the constraint map.

Regards


Top
 Profile  
 
 Post subject: Re: Dynamically selecting fields which fields to validate
PostPosted: Thu Jul 10, 2014 6:57 am 
Hibernate Team
Hibernate Team

Joined: Sat Jan 24, 2009 12:46 pm
Posts: 388
In this case the API for programmatic constraint declaration (https://docs.jboss.org/hibernate/stable/validator/reference/en-US/html_single/#section-programmatic-api) may be a solution for you. Based on the configuration stored in the CMS you'd configure the constraints applying to your types and bootstrap a ValidatorFactory using that configuration. You should cache it somewhere as its creation is costly. Then when your CMS configuration changes, i.e. additional constraints have been set up etc. - you'd re-create the ValidatorFactory.

Hth,

--Gunnar

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


Top
 Profile  
 
 Post subject: Re: Dynamically selecting fields which fields to validate
PostPosted: Thu Jul 10, 2014 9:18 am 
Newbie

Joined: Tue Jul 08, 2014 12:04 pm
Posts: 3
I will give this approach a try and report back.

Thanks again


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