8 Replies Latest reply on Jan 20, 2008 4:10 AM by Javier Garcia

    Problem with validation and entityHome

    Javier Garcia Newbie

      Hi everybody, first overall I' m newbie in seam development.
      I've developed an application generated with seamgen JBOSS-SEAM 1.2.1-GA.
      The application has a ReportsHome class that extends from EntityHome.

      public class ReportsHome extends EntityHome<Reports> {
      ...
      }
      

      On the model in the Reports class i ve written a constraint
       @AssertFalse(message="You must assign an author before close the report")
       public boolean isok(){
       return (authors.getId()==0 && reportstatus.getId()>=4);
       }
      

      In my view i ve put a tag s:validateAll that enclose all the items that must be validated.
      In theory i think it is all to use validation in seam.
      But when isok is true the application crashes and go to the debug page. The expected behavior is the message that inform about the assert error.
      There is something wrong in the application?
      Thanks in advance.

        • 1. Re: Problem with validation and entityHome
          Matt Drees Master

          s:validateAll only checks field-level validation, not class-level validation, which includes @AssertFalse.

          Currently, the simplest way (IMO) to validate multiple fields is to put some logic in your action method. The booking example does this with regards to checkin and checkout dates, IIRC.

          • 2. Re: Problem with validation and entityHome
            Javier Garcia Newbie

            Thanks for ur reply matt.drees.
            I try writing a new method mysave in ReportsHome:

             public String mysave(){
             if (this.getInstance().getRadiologos().getId()==0 && this.getInstance().getReportstatus().getId()>=4){
             this.addFacesMessage("ERROR");
             return "error";
             }else{
             super.update();
             return null;
             }
             }
            

            In the JSF page the method is called by a <s:button>
             <s:button id="mysave"
             value="mysave"
             action="#{reportsHome.mysave}"
             view="/Reports.xhtml"
             rendered="#{reportsHome.managed}"/>
            


            The behaviour is: the values are saved even when the ERROR message is raised and the application goes to "Reports.xhtml" page, even when the parameters aren't valid and the super.update method is not called.
            Why?
            How could I do it for don't save it when the values are not correct?
            Thanks in advance.

            • 3. Re: Problem with validation and entityHome
              Matt Drees Master

              By default, changes made to managed objects are flushed upon transaction completion, even if an update() is never manually called.

              You probably want to use manual flush mode. It's in the docs.

              There are also other posts about it.

              • 4. Re: Problem with validation and entityHome
                Javier Garcia Newbie

                thanks for your quick reply.
                I try it putting in the ReportsEdit.page.xml

                <begin-conversation join="true" flush-mode="MANUAL"/>

                But the behavior is the same.
                Is it ok?
                Thanks

                • 5. Re: Problem with validation and entityHome
                  Matt Drees Master

                  Oh, maybe I misunderstood the situation.

                  Did you appropriately change the page.xml file? (ReportsHome.page.xml, iirc)

                  Post what you have.

                  • 6. Re: Problem with validation and entityHome
                    Javier Garcia Newbie

                    Ok.
                    I have:
                    -JSF page: ReportsEdit.xhtml with a button

                     <s:button id="mysave"
                     value="mysave"
                     action="#{reportsHome.mysave}"
                     view="/Reports.xhtml"
                     rendered="#{reportsHome.managed}"/>
                    
                    

                    -page xml: ReportsEdit.page.xml
                    <!DOCTYPE page PUBLIC
                     "-//JBoss/Seam Pages Configuration DTD 1.2//EN"
                     "http://jboss.com/products/seam/pages-1.2.dtd">
                    
                    <page no-conversation-view-id="/ReportsList.xhtml"
                     login-required="true">
                    
                     <begin-conversation join="true" flush-mode="MANUAL"/>
                    
                     <action execute="#{reportsHome.wire}"/>
                    
                     <param name="reportsFrom"/>
                     <param name="reportsStudyuid" value="#{reportsHome.reportsStudyuid}"/>
                     <param name="patientsFrom"/>
                     <param name="patientsId" value="#{patientsHome.patientsId}"/>
                    
                     <param name="radiologosFrom"/>
                     <param name="radiologosId" value="#{radiologosHome.radiologosId}"/>
                    
                     <param name="reportstatusFrom"/>
                     <param name="reportstatusId" value="#{reportstatusHome.reportstatusId}"/>
                    
                    
                     <navigation from-action="#{reportsHome.persist}">
                     <end-conversation/>
                     <redirect view-id="/Reports.xhtml"/>
                     </navigation>
                    
                     <navigation from-action="#{reportsHome.update}">
                     <end-conversation/>
                     <redirect view-id="/Reports.xhtml"/>
                     </navigation>
                    
                     <navigation from-action="#{reportsHome.remove}">
                     <end-conversation/>
                     <redirect view-id="/PatientsQR.xhtml"/>
                     </navigation>
                    
                    </page>
                    


                    -Action .java: ReportsHome.java
                    public String mysave(){
                     if (this.getInstance().getRadiologos().getId()==0 &&
                     this.getInstance().getReportstatus().getId()>=4){
                     this.addFacesMessage("ERROR");
                     return "error";
                     }else{
                     super.update();
                     return null;
                     }
                    }
                    


                    That's all.
                    Thanks for ur help.

                    • 7. Re: Problem with validation and entityHome
                      Matt Drees Master

                       

                      "fjgarmu" wrote:

                       <s:button id="mysave"
                       value="mysave"
                       action="#{reportsHome.mysave}"
                       view="/Reports.xhtml"
                       rendered="#{reportsHome.managed}"/>
                      
                      



                      I think there a couple problems here; I should have noticed them earlier.

                      For one thing, do you really want an s:button? Usually for "save" kind of controls, you want an h:commandButton, because you want to submit some data, not just trigger an action.

                      If you do in fact want an s:button, you shouldn't specify both "action" and "view". (I think.) In this case, you want the action, but not the view attribute.

                      Also, you'll need a navigation rule in your ReportsEdit.page.xml for your "mysave" action that goes to the ReportsList page if the outcome isn't "error".
                      (Note that the typical JSF pattern, contrary to your design, is to return null if you want to stay on the same page and a string like "success" if you want to move away. So you may consider doing that instead.)

                      • 8. Re: Problem with validation and entityHome
                        Javier Garcia Newbie

                        Thanks for ur answer.
                        I' ve changed the <s:button> by <h:commandButton> and in the page.xml I ve written a navigation rule:

                         <navigation from-action="#{reportsHome.mysave}">
                         <rule if-outcome="success">
                         <end-conversation/>
                         <redirect view-id="/Reports.xhtml"/>
                         </rule>
                         </navigation>
                        

                        It works fine.

                        Thanks a lot.