• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

validations on dates

 
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

There is a screen which has 2 date fields. I need to provide the validation on these 2 date fields. And this screen also having the fetch button whihc has a ajax functionality to retrive the records between these two dates and get displayed into data table.

I need to provide validation on click on fetch button for these two fields and should displayed top of the screen. But i would like to use default jsf date validattions .Can anyone suggest me how this can be done..




public fetchResults(){

// I would like to catch the exceptions if user enters wrong dates. or the range between two dates should be 2 months.

}

How do i catch the exceptions when we click the fetch button if the user enters invalide date or range between two dates should be 2 months.


Thanks
Prasad
 
Saloon Keeper
Posts: 27762
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In cases where the user enters a syntactically-invalid date, JSF's validators will intervene and short-circuit the JSF lifecycle, bypassing the property updating and the action method.

In cases where the problem is semantic (date range is invalid), you have 2 choices: 1) write a custom validator or 2) perform the user-defined constraint checking in the action method and alter the action accordingly.

The main problem with writing a custom validator is that historically, validators operated only on a single control. You CAN create a validator that references another control, but it was a more annoying task to program. Recent changes to JSF have made that a little easier, however.

In the case where the semantic validation is done in the action method, you would code something like this:


Note that I interjected my general-purpose "JSFUtils" class in order to keep a lot of JSF-specific ugliness out of the general code. All the addErrorMessage method does is construct a JSF error message object and add it to the JSF messages collection.

Also I invented a date-differencing method named "dayDifference". In its simplest form, it would just get the times from date1 and date2 and see if their difference exceeded 1000*3600*24*14 milliseconds, but you could also use the Calendar classes. And in cases where you want differences that aren't always contstant, such as between 2 days in varying-length months, you'd pretty much have to.
 
prasad kakani
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Tim,

Great thanks for your reply first.

I am using some datepicker javascipt code, to display the date , when the user clicks on the date field text box.
I need to validate this date whether this date is correct or not. Can you please let me know , how i could use jsf built in date validations like <f:convertDateTime >?

And all error messages whatever thrown in bean should be captured and displayed header of the screen.For this i am using <h:messages > tag.

I tried this approch.



And ajax listener method is



No error messages coming on the screen when i click empty data. Request you please clarify once?


Thanks
Prasad.
 
Tim Holloway
Saloon Keeper
Posts: 27762
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ConvertDateTime is not a validator, it is a converter. Its primary function is to convert to/from text (html) values to internal Date or Calendar binary object values.

When a control's input is failed by a JSF validator - whether it's a built-in one or not - the ValidatorException is captured by the FacesServlet and a message is added to the JSF messages collection, which is what is displayed by the h:messages tag.

In the example I gave, I used a utility class of my own design to generate a FacesMessage object and add it to the JSF messages, but I could have simply constructed and thrown a ValidatorException. The main differences are that an exception immediately exits the method that generates it (subject to try/catch/finally logic), which means that if I wanted multiple messages, I would have to use a less extreme technique. And a ValidatorException is an Error-level message, so when I want less urgent JSF messages I have to use alternatives. My standard JSFUtils class has alternative methods such as "addInfoMessage", for example.
 
prasad kakani
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hi Tim,

Thanks for your quick response. Could you please elaboratly explain your JSFUtils ?

Even i tried this approch:






The error is coming on the console but it is not getting in the page.
Could you please clarify the above one?

And one more is Can you please suggest me how do i validate date in the bean method? will JSF provide any built in validation for Date.?

Thanks
Venkat
 
Tim Holloway
Saloon Keeper
Posts: 27762
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
JSFUtils is a static class that I define to hold JSF-specific code so that I don't have to splatter javax.faces dependencies all over my backing beans. It makes the beans more portable (POJO), makes them easier to do offline unit testing, and simplifies and abstracts things like adding info and error messages to the FacesContext.

So far, I've been building copies of this class on a per-application basis, so I don't have a standard implementation. But as a rule, it usually contains methods to work with messages and to access the HttpServletRequest, Response, and Session objects.

If you code "required="true"" on your dates, JSF will ensure that they will never be null without you having to code an explicit check. Actually, since NULL isn't valid HTML, a literal check for NULL isn't sufficient, since a blank string may be returned instead. Making a value required eliminates that problem as well. Or, if you have included the validation annotation support facility, you can annotate the dates with the @NotNull annotation.

You didn't indicate what the base class for MyDateException is. If it's not ValidatorException or some other subclass of FacesException, JSF won't intercept it.

If the date isn't in the expected format, the DateTimeConverter will throw an exception and the AJAX action method will never be called.

Finally, I believe that the AjaxBehaviorEvent parameter is optional. If you don't use it, you can leave it out.
 
prasad kakani
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Tim,

Still i am in error mode.






And my Date exception is



Even this alos not working.Can you please verify once..?

Thanks
VEnkat
 
Tim Holloway
Saloon Keeper
Posts: 27762
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am not clear on what the current error is. As I said, you probably won't get a null value on dates that were submitted without values being set, but will instead get an empty string, so your validation wouldn't be correct anyway. Besides, you can code "required="true"" on the xhtml and not have to code any Java logic at all.

 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic