aspose file tools*
The moose likes JSF and the fly likes Validator or just a normal function? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSF
Bookmark "Validator or just a normal function?" Watch "Validator or just a normal function?" New topic
Author

Validator or just a normal function?

Scott Stephens
Greenhorn

Joined: Jun 25, 2011
Posts: 23

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

Joined: Jun 25, 2011
Posts: 23

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?

Volodymyr Levytskyi
Ranch Hand

Joined: Mar 29, 2012
Posts: 505
    
    1

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!


True person is moral, false is right!
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16134
    
  21

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.


Customer surveys are for companies who didn't pay proper attention to begin with.
Scott Stephens
Greenhorn

Joined: Jun 25, 2011
Posts: 23

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:

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Validator or just a normal function?