• 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Validator or just a normal function?

 
Greenhorn
Posts: 23
Eclipse IDE Tomcat Server Debian
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The following jsf code has a start date and end date for applying for vacation.
the commandButton has an action="#{urlaubHandler.saveUrlaub}" to submit these dates.

I need to validate the dates that

1. start is before the end date
2. the dates have work days >0
3. the employee has enough vacation days on their account
4. there are no overlapping days with other vacation

My question is : Can I do all of the above checking in the function "saveUrlaub" If yes, can I throw a validator exception to aquire error messaging for the different error cases without using a bound validator in the jsf page?

 
Scott Stephens
Greenhorn
Posts: 23
Eclipse IDE Tomcat Server Debian
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm still working on this. This function checks the dates to make sure some conditions are met. It is the action of the code already posted above. When I do the validation here, I can access the fields Uvon and Ubis. If I try to validate the input with the validator I can only access the Object value from the validator signature. But I need to compare it to see if the dates are valid and these belong together. Using the function saveUrlaub all my needs are met except being able to create a validation message!
How can I throw an exception that lands in the <h:message></h:message> tags?

 
Ranch Hand
Posts: 514
1
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello!

Using the function saveUrlaub all my needs are met except being able to create a validation message!


Any action listener method (including saveUrlaub) is invoked at Invoke Application Phase.
This phase is processed by jsf lifecycle only if any previous phase didn't throw Exception.
Validation is done at Process Validations Phase, conversion at Update Model Values phase. Both phases are processed before Invoke Application Phase

Custom validating method must be assigned to attribute 'validator', validating class @FacesValidator can be attached to component
via the same 'validator' attribute or <f:validator validatorId='myValidator' /> or programmatically!
If your validating method fails it should throw ValidatorException (there is also ValidationException but I don't know the difference, they both work well):
If you use atribute 'validator' there is also attribute 'validatorMessage' whose value is displayed if VAlidatorException is thrown.
To throw ValidatorException programmatically I use:
throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_ERROR, LocaleBean.getString("age_notvalid"), "mydetails"));
LocaleBean.getString("age_notvalid") - this is displayed at <h:message> element.
I hope this is what you need!
 
Saloon Keeper
Posts: 28319
210
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
Business validations rarely work well using JSF validator objects, because of the fact that the rules are context-sensitive and the JSF validators prefer to deal in absolutes.

ActionListeners give no benefit. It is sufficient to code the business validation login in an action method in most cases. Rather than attempting to throw a valiatorexception, I simply post a user-defined JSF message and abort the action, like so:


JSFUtils is just a catch-all class that I use to offload JSF-specific code into. It simplifies coding things like adding error messages or meddling around with J2EE objects and it makes testing easier, since I can swap out an emulated version when I'm running offline tests and don't actually have JSF running.

The technique I use of putting a bunch of tests that can bail out before I do the actual processing in a method isn't one I've ever seen formally blessed anywhere, but it gives a method to my madness. I call it "filtered processing", since it filters out conditions that would make processing impossible (or at least invalue). All the filters are done FIRST, followed by the processing code. It's similar to what Exceptions do, except that the mechanisms are less extreme. And, of course, Exceptions - since they are exceptional - are often not as easy to limit to the prolog part of a method.
 
Scott Stephens
Greenhorn
Posts: 23
Eclipse IDE Tomcat Server Debian
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Before I read the replies, I tried an example line from Bernd Müller's Java Server Faces "ein Arbeitsbuch für die Praxis"
It is more the direction that Tim suggested, keeping the business code and not using an action listener and looks like this:

 
Wink, wink, nudge, nudge, say no more, it's a tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic