1 2 Previous Next 17 Replies Latest reply on Sep 8, 2010 2:16 AM by Tim Evers

    Validation for rich:calendar

    Eswaramoorthy S Newbie

             Please any body help me ....How to validate from and to rich:calendar date.
       
      I want validation like if fromdate is greater than todate,then it display an error.



      Thanks
      Eswar 

        • 1. Re: Validation for rich:calendar
          Aboo salimon Newbie

          Eswar


          what is error?but You can validate in action class level.

          • 2. Re: Validation for rich:calendar
            Eswaramoorthy S Newbie
            This is my code in projectDetails.xhtml

            <s:decorate id="fromdateField" template="/data/layout/edit.xhtml">
            <ui:define name="label"><font color="blue"><i>From date </i></font></ui:define>
                   <rich:calendar id="fromDate"
                               styleClass="normalFont"
                               datePattern="dd/MM/yyyy"> 
                    <a:support event="onblur" reRender="fromdateField" bypassUpdates="true" ajaxSingle="true"/>
                   </rich:calendar>
            </s:decorate>
                        
            <s:decorate id="todateField" template="/data/layout/edit.xhtml">
                        <ui:define name="label"><font color="blue"><i>To date </i></font></ui:define>
                             <rich:calendar id="toDate"
                                            styleClass="normalFont"
                                            datePattern="dd/MM/yyyy"> 
                            
                                    <f:validator validatorId="DateValidation" />
                                    <f:attribute  name="Datefield"  value=" projectDetails:fromdateField:fromDate" />
                                    <a:support event="onblur" reRender="todateField" bypassUpdates="true" ajaxSingle="true"/>
                             </rich:calendar>
                         </s:decorate>




            DateValidator.java


            public class DateValidator implements Validator
            {
                 public void validate(FacesContext context, UIComponent comp, Object val)throws ValidatorException
                 {
            String mindate = (String) comp.getAttributes().get("Datefield");
                   
                   
                  UIInput dateInput = (UIInput) context.getViewRoot().findComponent(mindate);

               
            java.util.Date fromDate = (java.util.Date) dateInput.getValue();
                   
                    
                    Date toDate = (Date) val;
                  
              System.out.println("From date  " + fromDate);
              System.out.println("To date "  + toDate);
              
               
            }

            }





            The error is


            13:10:22,046 ERROR [lifecycle] JSF1054: (Phase ID: PROCESS_VALIDATIONS 3, View ID: /analyzer/projectDetails.xhtml) Exception thrown during phase execution: javax.faces.event.PhaseEvent[source=com.sun.faces.lifecycle.LifecycleImpl@e1b3b3]
            13:10:22,092 ERROR [Exceptions] handled and logged exception
            javax.servlet.ServletException
            • 3. Re: Validation for rich:calendar
              Tim Evers Master

              What you gave us is only a small part of the problem. Those error messages say that an exception happened. Where is the stack trace of the exception that occurred.


              But, for starters let's look at what a validator is. It determines if a value is VALID. They are not really for business rules or cross component rules (In JSF 1.x). A validator should answer 1 question and 1 question only. Did the user enter a valid value that is able to be processed by the application. So for all intents and purposes what you are trying to do is not natively supported.


              That being said, there may be ways to get the functionality you wanted but, the question still is should you do it this way.


              Check out This link it will give a few options. I think you'll have to give up on the idea of bypassUpdate=true though.


              Thankfully in JSF 2 this problem is solved in a relatively nice way :)


              Ultimately, you may have to just validate it in the action method of whatever button gets pressed.

              • 4. Re: Validation for rich:calendar
                Eswaramoorthy S Newbie
                In my code
                    <s:validateForm validatorId="DateValidator">

                not works

                Please give me a suggestion from above code.


                • 5. Re: Validation for rich:calendar
                  Tim Evers Master

                  Eswaramoorthy S wrote on Sep 03, 2010 05:00:


                  In my code

                  <s:validateForm validatorId="DateValidator">
                  



                  not works

                  Please give me a suggestion from above code.





                  Take the time to read the articles I linked to you properly and make sure that you are using the versions of the libs that it says you have to use in order to use that tag. It only works with JSF 2.0 and the appropriate version of Seam (I haven't checked what version but probly 3.x)


                  If you are not using JSF 2.0 then that is not a solution you can use. Go back to my first link and follow what it says.


                  Better yet, just save yourself the time and effort and validate that the dates are in order AFTER the user clicks a button. Much easier and generally speaking the normal/proper way to do what you are doing.

                  • 6. Re: Validation for rich:calendar
                    Marcio Dantas Newbie

                    Eswar,


                    i don't know if findComponent trims the client id.
                    Notice that you have a space char at the beginning of the id: projectDetails:fromdateField:fromDate.
                    If the component is not found, then you'll have a NullPointerException in your code at the next line.



                    Another thing that I've noted is that you're using ajaxSingle.
                    That means that only the component toDate will be fully processed in the JSF lyfe cycle. projectDetails:fromdateField:fromDate won't!


                    Maybe you should be using process attribute to include the other field in lyfe cycle processing.


                    Try to get the real error thats ocurring!


                    regards



                    • 7. Re: Validation for rich:calendar
                      Eswaramoorthy S Newbie
                      Hi Tim Evers
                               
                                I found a solution and i got proper error message by using this
                      <s:decorate id="fromdateField" template="/data/layout/edit.xhtml">
                      <ui:define name="label"><font color="blue"><i>From date </i></font></ui:define>
                             <rich:calendar id="fromDate"
                                     styleClass="normalFont"
                                     datePattern="dd/MM/yyyy"
                                      value="#{project.fromDate}">
                          <a:support event="onblur" reRender="fromdateField" bypassUpdates="true" ajaxSingle="true"/>
                             </rich:calendar>
                      </s:decorate>
                                <s:decorate id="todateField" template="/data/layout/edit.xhtml">
                              <ui:define name="label"><font color="blue"><i>To date </i></font></ui:define>
                                   <rich:calendar id="toDate"
                                                  styleClass="normalFont"
                                                  datePattern="dd/MM/yyyy"
                                                  value="#{project.toDate}">
                                                        <f:validator validatorId="DateValidation" />
                                          <f:attribute name="Datefield" value=" projectDetails:fromdateField:fromDate" />
                                          <a:support event="onblur" reRender="todateField" bypassUpdates="true" ajaxSingle="true"/>
                                   </rich:calendar>
                               </s:decorate>



                      DateValidator.java



                      public class DateValidator implements Validator
                      {
                           public void validate(FacesContext context, UIComponent comp, Object val)throws ValidatorException
                           {
                                
                                
                              String minId = (String) comp.getAttributes().get("Score");
                              UIInput passwordInput = (UIInput) context.getViewRoot().findComponent(minId);
                              Date fromDate = (Date) passwordInput.getValue();
                            
                                
                                
                              
                              Date toDate = (Date) val;
                             
                         if(fromDate==null || toDate==null)
                         {
                              throw new ValidatorException(new FacesMessage("*  Date Required" ) );
                         }
                       
                         else if(fromDate.getDate() > toDate.getDate() || fromDate.getMonth()> toDate.getMonth() || fromDate.getYear()>toDate.getYear())
                                  {
                           
                               throw new ValidatorException(new FacesMessage("* To date must be greater than From date" ) );
                       
                        }
                        
                         
                      }
                      }



                      But i have only problem
                      here
                      fromDate==null works fine and display the error message.

                      but
                      toDate==null doesn't works it doesn't display error message.






                      Please give me a suggestion


                      Thanks

                      Eswar



                      • 8. Re: Validation for rich:calendar
                        Tim Evers Master

                        Something to note before I try to work out your problem.


                        else if(fromDate.getDate() > toDate.getDate() || fromDate.getMonth()> toDate.getMonth() || fromDate.getYear()>toDate.getYear())
                        



                        That validation is wrong. You have basically said that if the user enters 31 March 2000 in the from date and then puts 01 Jan 2001 you will tell them that their from date is greater then their to date. So you might want to do proper date comparison instead of writing your own. Use date.compareTo to do your comparisons or something like that instead of comparing the individual fields of the date. It is a much better way of doing it and you will avoid the logic flaws you have in your current implementation.


                        Also, did you give us the right code?


                        String minId = (String) comp.getAttributes().get("Score");
                        UIInput passwordInput = (UIInput) context.getViewRoot().findComponent(minId);
                        



                        That doesn't make sense...(I could be missing something but...according to your xhtml you have an attribute called Datefield but here in your code you are looking up an attribute called Score? Where is this defined? Whey then are you calling your UIInput passwordinput. Try to make your variable names meaningful so your code is easier to read.


                        Now, your problem. When you say it doesn't work you need to start giving more information. What is happening. Does the exception get thrown or not? (Debug or write to a log file to tell you if the exception is getting thrown). If it gets thrown but the error doesn't appear then it is completely different problem to if the exception is not getting thrown at all.

                        • 9. Re: Validation for rich:calendar
                          Eswaramoorthy S Newbie
                          Thanks Tim.
                          Now i understood that problem and cleared it also


                          This is my code in xhtml


                          <s:decorate id="fromDateField" template="/data/layout/display.xhtml">
                          <ui:define name="label">From Date: </ui:define>

                          <rich:calendar id="processDate"
                                         value="#{projectList.project.processDate}"
                                      locale="#{locale}"
                                         popup="true"
                                         datePattern="dd/MM/yyyy"
                                      styleClass="normalFont">
                          </rich:calendar>
                          </s:decorate>
                                          
                          <s:decorate id="toDateField" template="/data/layout/display.xhtml">
                          <ui:define name="label">To Date: </ui:define>

                          <rich:calendar id="toDate"
                                         value="#{projectList.project1.toDate}"
                                      locale="#{locale}"
                                         popup="true"
                                         datePattern="dd/MM/yyyy"
                                      styleClass="normalFont">
                              <f:validator validatorId="DateValidator" />
                              <f:attribute  name="Score"      
                                            value="projectSearch:fromDateField:processDate"/>
                                                  
                          </rich:calendar>
                          </s:decorate>



                          This is my Java validator


                          public class DateValidator implements Validator
                          {
                               public void validate(FacesContext context, UIComponent comp,   
                                                       Object val)throws ValidatorException
                               {
                                    
                               String minId = (String) comp.getAttributes().get("Score");
                                  UIInput passwordInput = (UIInput)
                                                    context.getViewRoot().findComponent(minId);
                                  Date fromDate = (Date) passwordInput.getValue();
                                  Date toDate = (Date) val;
                                 
                                  if(fromDate==null || toDate==null)
                                  {
                                  throw new ValidatorException(new FacesMessage("* Date
                                                                                    Required" ) );
                                  }
                            
                            

                                  int results = fromDate.compareTo(toDate);

                            
                                  if(results > 0)
                                 {
                                  throw new ValidatorException(new FacesMessage("* To date  
                                                                 must be greater than From date"));
                                 }
                             }
                          }



                          From the above java Validator


                          fromDate==null works fine and display the error message.

                             Display error like "* Date Required" which is given in FacesMessage.
                          but
                          toDate==null doesn't works it doesn't display an error message.

                          if toDate not given it doesn't display an error message.

                          It display results from fromDate to latest date in database.

                          it doesnt validate "toDate==null"





                          Please give me a suggestion


                          Thanks

                          Eswar



                          • 10. Re: Validation for rich:calendar
                            Eswaramoorthy S Newbie

                            Thanks Tim.
                            Now i understood that problem and cleared it also


                            This is my code in xhtml


                            <s:decorate id="fromDateField" template="/data/layout/display.xhtml">
                            <ui:define name="label">From Date: </ui:define>

                            <rich:calendar id="processDate"
                                           value="#{projectList.project.processDate}"
                                           locale="#{locale}"
                                           popup="true"
                                           datePattern="dd/MM/yyyy"
                                           styleClass="normalFont">
                            </rich:calendar>
                            </s:decorate>
                                            
                            <s:decorate id="toDateField" template="/data/layout/display.xhtml">
                            <ui:define name="label">To Date: </ui:define>

                            <rich:calendar id="toDate"
                                           value="#{projectList.project1.toDate}"
                                           locale="#{locale}"
                                           popup="true"
                                           datePattern="dd/MM/yyyy"
                                           styleClass="normalFont">
                                <f:validator validatorId="DateValidator" />
                                <f:attribute  name="Score"      
                                              value="projectSearch:fromDateField:processDate"/>
                                                       
                            </rich:calendar>
                            </s:decorate>



                            This is my Java validator


                            public class DateValidator implements Validator
                            {
                                    public void validate(FacesContext context, UIComponent comp,   
                                                         Object val)throws ValidatorException
                                    {
                                           
                                    String fromDateId = (String) comp.getAttributes().get("Score");
                                    UIInput fromDateInput = (UIInput)
                                                      context.getViewRoot().findComponent(fromDateId);
                                    Date fromDate = (Date) fromDateInput.getValue();
                                    Date toDate = (Date) val;
                                   
                                    if(fromDate==null || toDate==null)
                                    {
                                       throw new ValidatorException(new FacesMessage("* Date
                                                                                      Required" ) );
                                    }
                              
                              

                                    int results = fromDate.compareTo(toDate);

                              
                                    if(results > 0)
                                   {
                                       throw new ValidatorException(new FacesMessage("* To date  
                                                                   must be greater than From date"));
                                   }
                               }
                            }



                            From the above java Validator


                            fromDate==null works fine and display the error message.

                               Display error like "* Date Required" which is given in FacesMessage.
                            but
                            toDate==null doesn't works it doesn't display an error message.

                            if toDate not given it doesn't display an error message.

                            It display results from fromDate to latest date which is stored in database .

                            it doesnt validate "toDate==null"





                            Please give me a suggestion


                            Thanks

                            Eswar




                            • 11. Re: Validation for rich:calendar
                              Tim Evers Master

                              Eswaramoorthy S wrote on Sep 07, 2010 02:54:



                              Thanks Tim.
                              Now i understood that problem and cleared it also


                              This is my code in xhtml

                              <s:decorate id="fromDateField" template="/data/layout/display.xhtml">
                              <ui:define name="label">From Date: </ui:define>
                              
                              <rich:calendar id="processDate" 
                                             value="#{projectList.project.processDate}"
                                             locale="#{locale}"
                                             popup="true"
                                             datePattern="dd/MM/yyyy"
                                             styleClass="normalFont">
                              </rich:calendar>
                              </s:decorate>
                                               
                              <s:decorate id="toDateField" template="/data/layout/display.xhtml">
                              <ui:define name="label">To Date: </ui:define>
                              
                              <rich:calendar id="toDate" 
                                             value="#{projectList.project1.toDate}"
                                             locale="#{locale}"
                                             popup="true"
                                             datePattern="dd/MM/yyyy"
                                             styleClass="normalFont">
                                  <f:validator validatorId="DateValidator" />
                                  <f:attribute  name="Score"       
                                                value="projectSearch:fromDateField:processDate"/>
                                                          
                              </rich:calendar>
                              </s:decorate>
                              




                              This is my Java validator

                              public class DateValidator implements Validator
                              {
                                      public void validate(FacesContext context, UIComponent comp,    
                                                           Object val)throws ValidatorException
                                      {
                                              
                                      String fromDateId = (String) comp.getAttributes().get("Score");
                                      UIInput fromDateInput = (UIInput)
                                                        context.getViewRoot().findComponent(fromDateId);
                                      Date fromDate = (Date) fromDateInput.getValue();
                                      Date toDate = (Date) val;
                                      
                                      if(fromDate==null || toDate==null)
                                      {
                                         throw new ValidatorException(new FacesMessage("* Date Required" ) );
                                      }
                                 
                                 
                              
                                      int results = fromDate.compareTo(toDate);
                              
                                 
                                      if(results > 0)
                                     {
                                         throw new ValidatorException(new FacesMessage("* To date must be greater than From date"));
                                     }
                                 }
                              }
                              




                              From the above java Validator

                              fromDate==null
                              



                              works fine and display the error message.
                              Display error like * Date Required which is given in FacesMessage.

                              but

                              toDate==null
                              


                              doesn't works it doesn't display an error message.

                              if toDate not given it doesn't display an error message.

                              It display results from fromDate to latest date which is stored in database .

                              it doesnt validate toDate==null





                              Please give me a suggestion


                              Thanks

                              Eswar







                              Eswar, I need you to provide some more information so that we know what is going on.


                              In your validator log out the values that the validator is getting.


                              String fromDateId = (String) comp.getAttributes().get("Score");
                              UIInput fromDateInput = (UIInput)context.getViewRoot().findComponent(fromDateId);
                              Date fromDate = (Date) fromDateInput.getValue();
                              Date toDate = (Date) val;
                              System.out.println("fromDate value is: " + fromDate.toString() + ", toDate value is: " + toDate.toString());
                              



                              You may want to use a proper logger instead of System.out.println but whatever you do just make sure that you know what values the validator is actually dealing with. Sounds to me like you have some default value getting used or something. So do this and check that it actually IS getting a null value for the toDate field.


                              If your log tells you that you are indeed dealing with a null value then put another log line before you throw the ValidatorException that just says something like throwing ValidatorException because one of the dates was null. Let's start making sure that we are getting to the code that we expect it to get to. Maybe it is failing in a different way.


                              Check your server log and make sure there are no errors or warnings there. If there is then post them here to see if any of them might be related. Start using debug mode so that you can step through your code and make sure it is doing what you expect it do do.


                              When you've got the output of a log file showing the path that the code is executing then we can look further into the problem. Until then keep using logging or debugging until you know where the problem is and what is causing the problem.

                              • 12. Re: Validation for rich:calendar
                                Tim Evers Master

                                Gah, I write bad code too! Dont' use


                                System.out.println("fromDate value is: " + fromDate.toString() + ", toDate value is: " + toDate.toString());
                                



                                That will cause NullPointer exceptions.


                                use


                                System.out.println("fromDate value is: " + fromDate + ", toDate value is: " + toDate);
                                
                                



                                Let java work out whether or not to call toString() :P I mean all we care is whether the value is null or not right? :)

                                • 13. Re: Validation for rich:calendar
                                  Eswaramoorthy S Newbie
                                  Hi Tim



                                  If I select the rich:calendar value like

                                  FromDate: 01/09/2010
                                  ToDate : 30/09/2010

                                  Then the result in console is

                                  10:34:50,341 INFO  [STDOUT] Hibernate: select project0_.PROJECT_ID as PROJECT1_0_, project0_.CUSTOMER_ID as CUSTOMER6_0_, project0_.PROJECT_NAME as PROJECT2_0_, project0_.RE_DOC_ID as RE7_0_, project0_.PROCESS_OWNER as PROCESS3_0_, project0_.PROCESS_DATE as PROCESS4_0_, project0_.PROCESS_DATE1 as PROCESS5_0_ from pixeldb.project project0_ limit ?
                                  10:34:50,341 INFO  [STDOUT] fromDate value is: Wed Sep 01 00:00:00 IST 2010
                                  10:34:50,341 INFO  [STDOUT]  toDate value is: Thu Sep 30 00:00:00 IST 2010
                                  10:34:50,356 INFO  [STDOUT] Hibernate: select project0_.PROJECT_ID as PROJECT1_0_, project0_.CUSTOMER_ID as CUSTOMER6_0_, project0_.PROJECT_NAME as PROJECT2_0_, project0_.RE_DOC_ID as RE7_0_, project0_.PROCESS_OWNER as PROCESS3_0_, project0_.PROCESS_DATE as PROCESS4_0_, project0_.PROCESS_DATE1 as PROCESS5_0_ from pixeldb.project project0_ where project0_.PROCESS_DATE>=? and project0_.PROCESS_DATE<=? limit ?
                                  10:34:50,435 INFO  [STDOUT] Hibernate: select project0_.PROJECT_ID as PROJECT1_0_, project0_.CUSTOMER_ID as CUSTOMER6_0_, project0_.PROJECT_NAME as PROJECT2_0_, project0_.RE_DOC_ID as RE7_0_, project0_.PROCESS_OWNER as PROCESS3_0_, project0_.PROCESS_DATE as PROCESS4_0_, project0_.PROCESS_DATE1 as PROCESS5_0_ from pixeldb.project project0_ where project0_.PROCESS_DATE>=? and project0_.PROCESS_DATE<=? limit ?
                                  10:34:50,482 INFO  [STDOUT] Hibernate: select count(project0_.PROJECT_ID) as col_0_0_ from pixeldb.project project0_ where project0_.PROCESS_DATE>=? and project0_.PROCESS_DATE<=?



                                  If I select the rich:calendar value like

                                  FromDate: leave as empty
                                  ToDate : 30/09/2010

                                  Display error like "* Date Required" which is given in FacesMessage.

                                  Then the result in console is

                                  10:36:38,286 INFO  [STDOUT] Hibernate: select project0_.PROJECT_ID as PROJECT1_0_, project0_.CUSTOMER_ID as CUSTOMER6_0_, project0_.PROJECT_NAME as PROJECT2_0_, project0_.RE_DOC_ID as RE7_0_, project0_.PROCESS_OWNER as PROCESS3_0_, project0_.PROCESS_DATE as PROCESS4_0_, project0_.PROCESS_DATE1 as PROCESS5_0_ from pixeldb.project project0_ limit ?
                                  10:36:38,286 INFO  [STDOUT] fromDate value is: null
                                  10:36:38,286 INFO  [STDOUT]  toDate value is: Thu Sep 30 00:00:00 IST 2010
                                  10:36:38,442 INFO  [STDOUT] Hibernate: select count(project0_.PROJECT_ID) as col_0_0_ from pixeldb.project project0_


                                  If I select the rich:calendar value like

                                  FromDate: 01/09/2010
                                  ToDate : leave as empty

                                  No error message displays

                                  Then the result  in console only this

                                  10:33:31,623 INFO  [STDOUT] Hibernate: select project0_.PROJECT_ID as PROJECT1_0_, project0_.CUSTOMER_ID as CUSTOMER6_0_, project0_.PROJECT_NAME as PROJECT2_0_, project0_.RE_DOC_ID as RE7_0_, project0_.PROCESS_OWNER as PROCESS3_0_, project0_.PROCESS_DATE as PROCESS4_0_, project0_.PROCESS_DATE1 as PROCESS5_0_ from pixeldb.project project0_ limit ?
                                  10:33:31,638 INFO  [STDOUT] Hibernate: select project0_.PROJECT_ID as PROJECT1_0_, project0_.CUSTOMER_ID as CUSTOMER6_0_, project0_.PROJECT_NAME as PROJECT2_0_, project0_.RE_DOC_ID as RE7_0_, project0_.PROCESS_OWNER as PROCESS3_0_, project0_.PROCESS_DATE as PROCESS4_0_, project0_.PROCESS_DATE1 as PROCESS5_0_ from pixeldb.project project0_ where project0_.PROCESS_DATE>=? limit ?
                                  10:33:31,685 INFO  [STDOUT] Hibernate: select project0_.PROJECT_ID as PROJECT1_0_, project0_.CUSTOMER_ID as CUSTOMER6_0_, project0_.PROJECT_NAME as PROJECT2_0_, project0_.RE_DOC_ID as RE7_0_, project0_.PROCESS_OWNER as PROCESS3_0_, project0_.PROCESS_DATE as PROCESS4_0_, project0_.PROCESS_DATE1 as PROCESS5_0_ from pixeldb.project project0_ where project0_.PROCESS_DATE>=? limit ?
                                  10:33:31,779 INFO  [STDOUT] Hibernate: select count(project0_.PROJECT_ID) as col_0_0_ from pixeldb.project project0_ where project0_.PROCESS_DATE>=?




                                  In my project only toDate==null is not working other than this every thing is fine.

                                  I hope now you understand my problem.


                                  Thanks

                                  Eswar
                                  • 14. Re: Validation for rich:calendar
                                    Tim Evers Master

                                    I think I know the problem! Why I didn't realise this earlier I have no idea! Sorry but the reason why this is happening is quite simple!


                                    The validator will not get called if the value is NULL. There is no validation to be performed on a NULL value so the validation of that component is bypassed!


                                    I'm not sure exactly how to solve the problem just yet, but at least now you might be able to get down the right track. I'm looking into how other people have solved the problem. I'll get back to you if I find something. :)

                                    1 2 Previous Next