3 Replies Latest reply on Jun 22, 2012 6:54 AM by Hardy Ferentschik

    [Help] Bean Validation

    Ste Gr Newbie

      Hi,

       

      I'm learning JSF 2 and the new features that are coming with this version. Currently I'm testing the new bean validation and I'm facing a problem I'm unable to solve. Maybe it's the correct behaviour but I'm not able to find helpful references...

       

      I have a ViewScoped Managed Bean which contains a member variable of a custom class and in this class there is a variable named text with getter and setter and the validation constraint @Size(min=2). In a JSF document I'm using this variable in conjunction with a h:inputText.

       

      When I submit the form I expect to get an validation error when the text-length is less than 2, but nothing happens. If I move the variable text and their methods directly into the managed bean, everything works just fine.

       

      Is there any chance to validate even non managed-beans objects the same way as managed-beans by just annotating the class variables?

       

       

      @ViewScoped

      @Named

      class MyBean implements Serializable

      {

           private MyObj obj;

       

           public MyObj getObj()

           {

                return obj;

           }

       

           /* This would work...

           @Size(min=2)

           private String text;

       

           public String getText()

           {

                return text;

           }

       

           public void setText(String t)

           {

                text = t;

           }

           */

      }

       

      ---------------------------------------

       

      class MyObj

      {

           @Size(min=2)

           private String text;

       

           public String getText()

           {

                return text;

           }

       

           public void setText(String t)

           {

                text = t;

           }

      }

       

      ---------------------------------------

       

      <h:form>

           <h:inputText value="#{myBean.obj.text}" id="text" />

           <!-- h:inputText value="#{myBean.text}" id="text" / -->

           <h:message for="text" />

           <h:commandButton value="send" />    

      </h:form>

        • 1. Re: [Help] Bean Validation
          Hardy Ferentschik Novice

          Have you tried placing @Valid onto field or getter of MyObj?

          • 2. Re: [Help] Bean Validation
            Ste Gr Newbie

            I already tried @Valid.

             

             

            Edit:

            I did some debugging and found the root cause of the problem. In my real application my field is a private one and starts with an underscore. I put the annotation on this field directly. But my getter was named without this underscore (_text vs. getText()) and the validator implementation performs an equals() on those two names (getText() will be used as text). So _text != text and no validation will be performed... Good to know but in the end it's a logic process. How should the implementation know that getText() is returning _text.^^

             

            So at all others that stumble over this thread looking for an answer: Always annotate the getters or name the annotated variables the same as the getters

             

             

            Now all I have to do is to find out, why I don't get the error message. But that might be a wrong config. Thanks anyway Hardy

            • 3. Re: [Help] Bean Validation
              Hardy Ferentschik Novice

              Glad you found the answer :-)