I've been asked to write a custom component in JSF 2. The idea is the tag would take in several date attributes and do a sort of validation on the passed values.
I've written tags before, but in every other case the tags have contain some UIComponent that I can register a validator with. In this case, I can't think of a way to register a validator on the passed components. This is the awful component that I cooked up so far:
As you can see, I'm using EL to pass my component to a validator and letting the h:messages handle the exceptions I'm throwing. But I feel like there has to be a better way to do this. I guess this shows my lack of experience with JSF. I appreciate the help!
It's not recommended to write your own tags in JSF. If the standard JSF tags don't have, there's other libraries you can consider eg Primeface, ICEfaces, etc
There is syntactic validation (more than one decimal point in a number, text in a number, malformed date, and so forth), and then there is semantic validation (end date >= begin date, double password entry/check, etc.)
The JSF validators concentrate on syntactic validation, not semantic validation, considering semantic validation to be essentially part of the business logic, and thus more properly implemented in the action methods than in the View template. What I normally do in such cases is placed that code at the head of the action method and before the real business logic gets invoked, I do all my semantic validations, put messages (if needed) to JSF Messages, then short-circuit the business logic by immediately returning "null" to redisplay the page with the validation failures in the JSF Messages collection.
If you were to write a custom binary tag - and I don't recommend it for more reasons than just the above - you could take your cue from other decorator tags, which tend to either expect to be nested within the tag or use a tag id reference(s).
You wouldn't succeed in getting your validator example to work in any event. The Model doesn't get updated until/unless all validations have succeeded, so you'd be looking at the "before" values of the data being validated, not the current values in the form being submitted.
Science is the process of replacing what we "know" with what is TRUE. Politics, alas, often prefers to be the opposite.
Enjoy the full beauty of the english language. Embedded in this tiny ad: