5 Replies Latest reply on Mar 9, 2010 2:51 PM by Sergio Samayoa

    How to currently achieve Seam 2.2.0GA DRY form fields validation?

    Rodrigo Bento Newbie

      Hello,


      I would like to know how to currently achieve Seam 2.20GA DRY form fields validation in a elegant way?


      I'm stuck with that required-true JSF attribute issue, so in order to display nice messages in the UI, I have created a validate() method in my action bean which validates each attributes and put the EL messages from bundle into facesMessages component.


      I really hate looking at this validate() method knowing that it would not be needed if I could do DRY principle work, but searching on google and in forums I couldn't find a solution yet.


      I even tried to use some time ago Seam 2.1.2GA, JSR 303 and JSF 2.0 RI but I've got problems with Hibernate and I kinda had to put a Hibernate legacy api into classpath to fix that, but I didn't go ahead with that because both JSR 303 and JSF 2.0 were released recenttly and I felt it was not a good idea. I've read something about apache extval api too, but I didn't test it yet.


      I've read that in Seam 3 this DRY validation will work out-of-the-box, will we need to wait until there? Is there any idea about its release/


      How you guys currently using Seam deal with for validation without DRY in an elegant way?


      I'm asking because I really loved the concept but I got frustrated I wasn't able implement that.


      I'm currently developing a small webapp with Seam 2.20GA, Tomcat 6.0.24, PostgreSQL 8.4.2-1.


      I really appreciate the work that is being done on this framework!


      Thanks in advance,


      Rodrigo






        • 1. Re: How to currently achieve Seam 2.2.0GA DRY form fields validation?
          Sergio Samayoa Newbie
          Not quite sure if I understand well.
          Do you check manually required fields in "validate()" because you dont like standard message?
          If thats true, there is "requiredMessage" attribute in "inputText" JSF component which you can hard code in the view or use EL expression, for example (actual code):

          <s:decorate id="fld1" template="/inc/edit.xhtml">
               <ui:define name="label">Código</ui:define>
               <h:inputText id="ed1" required="true" requiredMessage="#{messages.depto_cod_req}"
                    value="#{deptoHome.instance.deptoCodigo}" size="10" maxlength="10"
                    label="Código">
                    <a4j:support event="onblur" reRender="fld1" bypassUpdates="true"
                         ajaxSingle="true" />
               </h:inputText>
          </s:decorate>

          Note that @NotEmpty annotation is not enforced until submit because if you dont enter anything in the field the validation isnt fired.

          Regards.
          • 2. Re: How to currently achieve Seam 2.2.0GA DRY form fields validation?
            Rodrigo Bento Newbie
            Sergio, thanks for the reply and the example.

            I tried your example and it worked here.
            You were correct, I do not like showing a message like this to the user: j_id62:title: Validation Error: Value is required.
            Now my bundle message for not empty items is correctly displayed on the screen. But doing this, strangely the messages for other validations like @Length started to work too. Do you know why is it happening? When using requiredMessage somehow JSF started looking for the messages in the model?

            Could you point me some link that explains that in detail?

            Thanks for your time.

            Rodrigo
            • 3. Re: How to currently achieve Seam 2.2.0GA DRY form fields validation?
              Sergio Samayoa Newbie
              Did you had "ajax support" before using the code I give to you?

              By the way, you can customize the JSF messages this ways:

              1. Override javax.faces.component.* keys in "messages.properties", for example:

              javax.faces.component.UIInput.REQUIRED=Por favor ingrese un valor, no puede dejarlo en blanco

              2. Add "label" attribute to your inputText components like the example I give to you. This will avoid the <parent component id>:<component id>:<message> like the one you show as example.

              3. Hard code the message in "requiredMessage" attribute.

              Regards.
              • 4. Re: How to currently achieve Seam 2.2.0GA DRY form fields validation?
                Rodrigo Bento Newbie

                Yes, I had ajax support enabled. Now I'm creating a template like your '/inc/edit.xhtml'.


                Well, it seems this subject will be easier to implement in Seam 3, with some improvements in JSF layer.


                Thanks for the hints, it's working now.


                Regards,


                Rodrigo

                • 5. Re: How to currently achieve Seam 2.2.0GA DRY form fields validation?
                  Sergio Samayoa Newbie

                  You are wellcome.
                  BTW, edit.xhtml is part of seam templates.
                  Regards.