5 Replies Latest reply on Sep 24, 2008 5:19 AM by Ingo Jobling

    Conditional hibernate validations?

    Jan Groth Novice

      Hello,


      simple questions: As the usage of hibernate validations is the best since toast in slices, I am wondering if there a way to express some sort of conditional validation at the entity bean.


      Consider the following situation:


      I have a field for an extra email, which can be provided but doesn't have to. If I annotated it with @Email, the empty field is valdiated against @Email, which obviously fails.


      Another example - a user can provide a city, but the field can also be empty. If it's not empty, I want min and max length checks to apply...


      Any solution for this?


      Many thanks,
      Jan


        • 1. Re: Conditional hibernate validations?
          Alex Savitsky Novice

          Which version of validator are you using? The latest one (3.1.0) certainly doesn't fail email validation on empty or null fields:


               public boolean isValid(Object value) {
                    if ( value == null ) return true;
                    if ( !( value instanceof String ) ) return false;
                    String string = (String) value;
                    if ( string.length() == 0 ) return true;
                    Matcher m = pattern.matcher( string );
                    return m.matches();
               }

          So you can annotate the extra email with @Email, and it will only fail if there's something in the field.


          Same story with the @Length validator - it won't fail on empty strings, either:


               public boolean isValid(Object value) {
                    if ( value == null ) return true;
                    if ( !( value instanceof String ) ) return false;
                    String string = (String) value;
                    int length = string.length();
                    return length >= min && length <= max;
               }


          Maybe it's the required="true" that's firing on your page, not HV?

          • 2. Re: Conditional hibernate validations?
            Alex Savitsky Novice

            Mental note to myself - code block formatting is NOT the same as Monospace/Code... sorry for the mess.

            • 3. Re: Conditional hibernate validations?
              Jan Groth Novice

              thanks for answering,


              what you are writing indicates that the problem is either the version of the validation framework or some parts of my business-logic.


              I have to check version and source code of the validation.jar. so far i can only tell that I'm using the hibernate-annotations.jar which came with either jboss or seam. i'm currently not able to determine the version.


              cheers,
              jan


              • 4. Re: Conditional hibernate validations?
                Alex Savitsky Novice

                Inside the HV jar, META-INF/MANIFEST.MF should contain the version number

                • 5. Re: Conditional hibernate validations?
                  Ingo Jobling Master

                  Hello,


                  The listing for @Length (posted) doesn't contain the statement


                  if ( string.length() == 0 ) return true;


                  Regards,
                  Ingo