-->
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.  [ 7 posts ] 
Author Message
 Post subject: Validation across fields
PostPosted: Fri May 27, 2011 8:13 am 
Newbie

Joined: Sun May 01, 2011 2:23 pm
Posts: 10
Hi,

I have been using springmodules validation but am now moving to hibernate validation.

With springmodules I had some checks across fields in the same class - for example field X must be >= field Y.

It is not clear to me the best way to achieve this with hibernate validation - after lots of webreading.

Can someone point me to a good example here.

Thanks,


Top
 Profile  
 
 Post subject: Re: Validation across fields
PostPosted: Fri May 27, 2011 10:31 am 
Newbie

Joined: Tue Mar 11, 2008 4:49 am
Posts: 12
I think you need to use class constraints.


Top
 Profile  
 
 Post subject: Re: Validation across fields
PostPosted: Sun May 29, 2011 5:30 pm 
Newbie

Joined: Sun May 01, 2011 2:23 pm
Posts: 10
I have tried to implement ‘class constraints’ but have hit an issue and would like advice on my implementation.

At present I receive the following text in the highTemp error field on an error condition.

{CustomTemperatureAlarms.temperatureAlarms}

and not the text in the resource bundle message.properties (see below).

I am not sure if I have implemented my ‘class constraint’ correctly to output a field level validation error.

Thanks for any help.


My class is:

Code:

@Entity
@CustomTemperatureAlarms(field="highTemp")
public class TemperatureAlarms extends Reading{
   
   @NotNull
   @Min(-30)
   @Max(70)
   private int highTemp = 50;
   @NotNull
   @Min(-30)
   @Max(70)
   private int lowTemp = 10;
   
   public void setHighTemp(int highTemp) {
      this.highTemp = highTemp;
   }
   public int getHighTemp() {
      return highTemp;
   }
   public void setLowTemp(int lowTemp) {
      this.lowTemp = lowTemp;
   }
   public int getLowTemp() {
      return lowTemp;
   }
   

}



My annotation interface is:

Code:

@Target(TYPE)
@Retention(RUNTIME)
@Constraint(validatedBy = CustomTemperatureAlarmsValidator.class)
@Documented
public @interface CustomTemperatureAlarms {

   String message() default "{CustomTemperatureAlarms.temperatureAlarms}";

    Class<?>[] groups() default {};

    Class<? extends Payload>[] payload() default {};
   
    String field();

}



My validator is:

Code:

public class CustomTemperatureAlarmsValidator implements ConstraintValidator<CustomTemperatureAlarms, TemperatureAlarms> {

   private String field;
   private String message;
   
   @Override
   public void initialize(CustomTemperatureAlarms customTemperatureAlarms) {
      field = customTemperatureAlarms.field();
      message = customTemperatureAlarms.message();
   }

   @Override
   public boolean isValid(TemperatureAlarms temperatureAlarms, ConstraintValidatorContext context) {
         
      if (temperatureAlarms.getLowTemp() <= temperatureAlarms.getHighTemp())
      {
                 
         return true;
      }
      
      context.disableDefaultConstraintViolation();
      context.buildConstraintViolationWithTemplate(message).addNode(field).addConstraintViolation();
      
         return false;
   }


}



And my JSP is:

Code:

<spring:message code="text.highTempAlarm"/>
    <form:select path="highTemp">     
    <c:forEach var="range" begin="0" end="100" step="1">
    <form:option value="${range-30}" label="${range-30}"></form:option>
    </c:forEach>
    </form:select>
    <form:errors path="highTemp" cssClass="errors"/>
<spring:message code="text.lowTempAlarm"/>
    <form:select path="lowTemp">     
    <c:forEach var="range" begin="0" end="100" step="1">
    <form:option value="${range-30}" label="${range-30}"></form:option>
    </c:forEach>
    </form:select>
    <form:errors path="lowTemp" cssClass="errors"/>



My message.properties has:

Code:

CustomTemperatureAlarms.temperatureAlarms=High < Low



Top
 Profile  
 
 Post subject: Re: Validation across fields
PostPosted: Tue May 31, 2011 7:04 am 
Newbie

Joined: Sun May 01, 2011 2:23 pm
Posts: 10
Extra information:

I am using:

<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>4.1.0.Final</version>
</dependency>

and if I replace:

context.buildConstraintViolationWithTemplate(message).addNode(field).addConstraintViolation();

with

context.buildConstraintViolationWithTemplate("text").addNode(field).addConstraintViolation();

The word "text" is output correctly.

So the problem appears to be with the finding the ‘resource bundle’ but I do have:

12:01:01,447 DEBUG PlatformResourceBundleLocator:71 - org.hibernate.validator.ValidationMessages found

in the trace output.


Top
 Profile  
 
 Post subject: Re: Validation across fields
PostPosted: Tue May 31, 2011 5:36 pm 
Hibernate Team
Hibernate Team

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

what's the name of your message bundle? It must be "ValidationMessages.properties" in case of a property based bundle (see also the HV reference guide).

The log output you see originates from loading HV's internal bundle ("org.hibernate.validator.ValidationMessages") which contains default messages to be used when no user-provided bundle ("ValidationMessages") exists.

Hth, Gunnar

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


Top
 Profile  
 
 Post subject: Re: Validation across fields
PostPosted: Tue May 31, 2011 6:13 pm 
Newbie

Joined: Sun May 01, 2011 2:23 pm
Posts: 10
Thanks.

What is still confusing me is that I am using Spring and have the bean below setup.

For the standard Hibernate validation errors eg. @NotNull my entries in the file message.properties are used.

It is just for the ‘class constraints’ logic above that it is not.

<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basename"> <value>messages</value></property>
</bean>


Top
 Profile  
 
 Post subject: Re: Validation across fields
PostPosted: Wed Jun 01, 2011 4:33 am 
Newbie

Joined: Sun May 01, 2011 2:23 pm
Posts: 10
Thinking about this more - maybe this is a Spring issue - I am using <mvc:annotation-driven> and this seems to pick up the correct "resource bundle" for the basic validation eg: @NotNull but not the 'class constraints'.

I will raise this on the Spring forum.

Thanks


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