-->
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: Field constraints ignored
PostPosted: Mon Aug 15, 2011 11:39 pm 
Newbie

Joined: Tue Mar 24, 2009 1:25 pm
Posts: 2
Location: Toronto
Hibernate Validator 4.1.0.Final, 4.2.0.Final
If the validation constraints are specified on the fields and a field name does not match the corresponding property name (e.g. property name is 'accountNumber' and field name is '_accountNumber' or 'm_accountNumber'), the validation constraints are ignored by JSF 2 validation.
The problem seem to be in ValidatorImpl.collectMetaConstraintsForPath method that has the following code:
...
for ( MetaConstraint<T, ?> metaConstraint : metaConstraintList ) {
if ( elem.getName() != null && elem.getName().equals( metaConstraint.getPropertyName() ) ) {
metaConstraints.add( metaConstraint );
}
}
...
I checked the JEE specs and it doesn't say anywhere that field name should match property name or that for JSF validation constraints should be declared on getters only. So, it looks like a bug to me.

Since there is no easy and reliable way to associate field to a property used in EL binding, the problem could be solved by providing a configuration parameter with list of prefixes, e.g. {"_", "m_"} in my case. I don't mind to commit the change if someone confirms that I'm on the right track.
To make it work for our project, I had to modify and re-build 4.1.0.Final with hard-coded prefix and I don't want to do it for all future releases.


Top
 Profile  
 
 Post subject: Re: Field constraints ignored
PostPosted: Tue Aug 16, 2011 5:37 am 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
Hi,

I think you are wrong. metaConstraint.getPropertyName() might be confusing though. It does not mean that we compare against the getter name. What the MetaConstraint stores is the JavaBean name of the constraint field or property. For a field it is the field name for a property (getter) is the name of the method with 'get' resp. 'is' stripped off.

I would assume that JSF is passing accountNumber to validateProperty and there is no constraint defined on the method nor is there a field with that name. Really JSF would have to pass '_accountNumber'.

I added a test to our codebase which shows that Validator really does not care that the field name and getter do not match.
Code:
   class NotFollowingJavaBeanNotation {
      @NotNull
      String m_foo;

      public String getFoo() {
         return m_foo;
      }
   }


Code:
   @Test
   public void testConstraintDefinedOnEntityNotFollowingBeanNotation() {
      CountValidationCallsValidator.init();
      Set<ConstraintViolation<NotFollowingJavaBeanNotation>> constraintViolations = getValidator().validate( new NotFollowingJavaBeanNotation() );

      // validating the whole entity - ok
      assertNumberOfViolations( constraintViolations, 1 );
      assertCorrectConstraintTypes( constraintViolations, NotNull.class );

      // using validateProperty (which is used by JSF) to validate explicitly using the field name
      constraintViolations = getValidator().validateProperty( new NotFollowingJavaBeanNotation(), "m_foo" );
      assertNumberOfViolations( constraintViolations, 1 );
      assertCorrectConstraintTypes( constraintViolations, NotNull.class );

           // using validateProperty (which is used by JSF) to validate explicitly (no violation, because there is no
      // property foo Validator knows about
      constraintViolations = getValidator().validateProperty( new NotFollowingJavaBeanNotation(), "foo" );
      assertNumberOfViolations( constraintViolations, 0 );
   }


I don't think there is a Validator issue here. WDYT?

--Hardy


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.