Well, custom validators belong to a real-life JSF web apllication like coffee to a good
start into the day ;).
Let's take an example (there are many occurences of such a situation in my current
app, but I love easy examples *g*):
- Entity Person
- has a n:m relation to multiple other persons (friends)
- has a relation to one specific person (best_friend)
When editing/creating a person you pick his/her friends from a list of persons
(SelectManyMenu) and the best friend, too, (but from a SelectOneMenu).
So far so good, that was easy. BUT: There is an important constraint: The best_friend must
be one of your friends, of course. So it's important to check this before saving.
There are two possibilities:
- In the action method ("store"): If best_friend is not contained in friends,
add an error message, mark the transaction as rollback-only and redisplay the page (empty
outcome: return null). Works, but it's UGLY
- In a custom JSF validator: BEAUTIFUL :)
Problem: The custom validator (that would be added to the best friend's SelectOneMenu)
needs not only be aware of the best friend, but also of the list of friends. It's a
"complex" validator (if I may call it like this), since it needs more
information than only the status of the field it belongs to.
Possible Solution: Add a validateBestFriend(FacesContext, UIComponent, Object) method to
the PersonEditorBean (SFSB), which holds the state of the edited person. So there the
validator method has access to all the information it needs and can perform the check. If
you select a "non-friend" as best friend, it throws a ValidatorException.
New problem: The ValidatorException is caught by the Stateful Bean and the app crashed
with an exception report. The ValidatorException is not passed through to JSF as EJB3
intervenes. DAMN!
So any idea how to make EJB3 ignore the Exception and pass it through? Or how to have such
a "complex" validation performed in an other way?
I'd be thankful for any answer :).
View the original post :
http://www.jboss.com/index.html?module=bb&op=viewtopic&p=3968586#...
Reply to the post :
http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&a...