posted 15 years ago
JSF was designed to support data validation as a discrete step in the JSF lifecycle. Validation done at this level ensures that no data is set in the backing bean unless all validators are satisfied - providing you don't bypass that phase by using an immediate action.
There are a small, but basically useful set of validators that come as part of the framework; for example, the RangeValidator. One validator is even built into selected JSF HTML tags themselves - the 'required="true"' validator.
Some of the third-party JSF tag libraries add additional validators, and there's a couple of efforts right now to extend validation even further by allowing annotation of the backing beans (the Myfaces Extensions Validator).
When none of the above will do, you can write your own validator. Implement a javax.faces.validator.Validator and code your validation in its validate() method. If validation fails, construct a FacesMessage object with the error message text in it and throw a ValidatorException. That's all there is to it.
Sometimes you have more complex validation issues such as when you have to cross-check properties against each other. The final line of defense is to code (or call) validation logic in your action processor and have it return a failure status. That's a last-resort method, since the backing bean has already been updates at that point, and most validators are intended to avoid setting improper values in the backing bean. The alternative is to create cross-checking validators that can lookup elements in the incoming update request tree, but that's wore work.
Education won't help those who are proudly and willfully ignorant. They'll literally rather die before changing.