-->
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.  [ 10 posts ] 
Author Message
 Post subject: Extra group validation based on domain property
PostPosted: Mon Apr 14, 2008 2:26 am 
Regular
Regular

Joined: Thu Oct 13, 2005 4:19 am
Posts: 98
Let's say we have a Budget class:

Code:
public class Budget {

  @NotNull(groups = "default")
  private BigDecimal budgetAmount;

  private boolean approved = false;
 
  @NotNull(groups = "approved")
  private Date approvalDate;

}


Now I 'd like to put an annotations on the "boolean approved" that if it is true, during the default validation it should also validate "approved".
Code:
  @WhenTrueAlsoValidateGroup(groups = "default", validateGroup = "approved")
  private boolean approved = false;


Currently in Hibernate Validator, I write @AssertTrue to verify that approvalDate isn't null when approved is true.

_________________
http://www.ohloh.net/accounts/ge0ffrey


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 23, 2008 8:04 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
I kinda like the idea, I am not sure how well it can be defined and implemented in a more generic way (just checking a boolean query is probably not specific enough). @NotNull(validateGroups="approved")

Do you want to explore the idea?

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 24, 2008 1:56 am 
Regular
Regular

Joined: Thu Oct 13, 2005 4:19 am
Posts: 98
Sure :) We'll see how far we can stretch this.

Here's an alternative proposal to this problem "activation-conditional-validation-1":


Code:
public class Budget {

  @NotNull(groups = "default")
  private BigDecimal budgetAmount;

  @ActivateConditionalValidationOnTrue(name = "whenApproved")
  private boolean approved = false;

  @ConditionalValidations(name = "whenApproved", values = {
    @NotNull()
  })
  private Date approvalDate;

}


This proposal doesn't use groups .

_________________
http://www.ohloh.net/accounts/ge0ffrey


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 24, 2008 2:05 am 
Regular
Regular

Joined: Thu Oct 13, 2005 4:19 am
Posts: 98
Yet another proposal: activation-conditional-validation-2

Code:
public class Budget {

  @NotNull(groups = "default")
  private BigDecimal budgetAmount;

  @ActivateConditionalValidation(name = "whenApproved", conditions= {
    @IsTrue()
  })
  private boolean approved = false;

  @ConditionalValidation(name = "whenApproved", validations = {
    @NotNull()
  })
  private Date approvalDate;

}


The main advantage of this proposal is how smart you can maybe make your InvalidValue's:
"Approval date must be not empty when approved is true"
approvalDate.label + IsNull.label + " when " + approved.label + IsTrue.label

_________________
http://www.ohloh.net/accounts/ge0ffrey


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 24, 2008 4:18 am 
Regular
Regular

Joined: Tue Apr 01, 2008 11:10 am
Posts: 69
I _think_ you can't do that with annotations. I'm also trying to push a proposal for conditional validation here: http://forum.hibernate.org/viewtopic.ph ... 95#2381895

I think my proposal would include what you want to do, and some more.
What do you think about it?

I'm all with you on that one: we need conditional validation.

_________________
--
Stéphane Épardaud


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 24, 2008 10:35 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
I think what I liked was the ability to reuse group names as the condition name.
Your proposal cannot compile in Java btw.

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 25, 2008 10:05 am 
Regular
Regular

Joined: Tue Apr 01, 2008 11:10 am
Posts: 69
emmanuel wrote:
I think what I liked was the ability to reuse group names as the condition name.
Your proposal cannot compile in Java btw.


I think my proposal compiles, I've compiled it. I'm not ruling out typos though ;)

_________________
--
Stéphane Épardaud


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 02, 2008 7:27 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Code:
@ConditionalValidation(name = "whenApproved", validations = {
    @NotNull()
  })
  private Date approvalDate;



this cannot compile if you expect to replace @NotNull with any annotation. Am I wrong?

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 03, 2008 2:23 am 
Regular
Regular

Joined: Thu Oct 13, 2005 4:19 am
Posts: 98
emmanuel wrote:
Code:
@ConditionalValidation(name = "whenApproved", validations = {
    @NotNull()
  })
  private Date approvalDate;



this cannot compile if you expect to replace @NotNull with any annotation. Am I wrong?


I don't know. Maybe it could be compiled by using Annotation as a parameter type:

Code:
/**
* The common interface extended by all annotation types.  Note that an
* interface that manually extends this one does <i>not</i> define
* an annotation type.  Also note that this interface does not itself
* define an annotation type. ...
*/
public interface Annotation {
   ...
    /**
     * Returns the annotation type of this annotation.
     */
    Class<? extends Annotation> annotationType();
}

But that won't guarantee the parameter type is a hibernate validator annotation class? Can a subset of annotations have a mutual abstract superclass?

_________________
http://www.ohloh.net/accounts/ge0ffrey


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 03, 2008 5:46 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
but then you cannot express the annotation attributes in a safe way you somehow need to use a key value pair

_________________
Emmanuel


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