-->
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: Custom Bean Annotation Problems
PostPosted: Wed Aug 18, 2010 4:20 pm 
Newbie

Joined: Thu Apr 29, 2010 3:05 pm
Posts: 4
This class level bean validation call is not being called.

Code:
@Constraint(validatedBy = FieldPairRequiredValidator.class)
@Target( {ElementType.TYPE, ElementType.CONSTRUCTOR} ) 
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface FieldPairRequired
{
    public abstract String message() default "When one field is specified, the second must be specified as well.";   

    public abstract Class<?>[] groups() default {};
   
    public abstract Class<? extends Payload>[] payload() default {};
   
    public String firstField();
   
    public String secondField();
}


Code:
public class FieldPairRequiredValidator implements ConstraintValidator<FieldPairRequired, Object>
{
   private String firstFieldName;
   private String secondFieldName;
   
   
   public void initialize(FieldPairRequired constraintAnnotation)
   {
      firstFieldName = constraintAnnotation.firstField();
      secondFieldName = constraintAnnotation.secondField();
   }
   
   
   public boolean isValid(Object value, ConstraintValidatorContext context)
   { 
      try
      {
         Object firstField = BeanUtils.getProperty(value, firstFieldName);
         Object secondField = BeanUtils.getProperty(value, secondFieldName);
         
         if((null == firstField && null != secondField) || (null == secondField && null != firstField))
         {
            return false;
         }
      }
      catch(Exception e)
      {
         System.err.println("Error: " + e);
         //ignore
      }
      
      return true; 
   } 
}


It is being called like this:

Code:
@FieldPairRequired(firstField = "id", secondField="secondId", message="Both Id and second Id must be specified when one is specified.")   
@ManagedBean 
@RequestScoped 
public class PersonSearch 


The only way I can get it to be called is when I place it on a field. I want it to validate a class not an individual field. Any ideas what is going on?


Top
 Profile  
 
 Post subject: Re: Custom Bean Annotation Problems
PostPosted: Thu Aug 19, 2010 10:57 am 
Newbie

Joined: Thu Apr 29, 2010 3:05 pm
Posts: 4
Any ideas on this? I am using Apache Tomcat 6 with hibernate-validator-4.1.0.Final.jar if that helps. I also can't get the annotation to work on a constructor. It only works on a field or method.


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.