JavaServer Faces 1.0 Early Access Draft (Page 5)
<< Page 4 (Responses and State Saving)
Validation and Error Messages
Section JSF 3.5 covers the Validator classes and how they are called during the JSF workflow as well as how they generate error messages and add them to the FacesContext. Error messaging has always been a major concern in all the projects I've participated in and both of the frameworks that I've written.
A reusable Validator is excellent because it reduces the amount of code duplication. However, it is very difficult to tailor messages for specific UIComponents using a reusable Validator.
For example, on one page I want a text box for age and on another one for income. I don't want my error messages to be generic stating that, "This value must be greater than 0 and less than X". I want the user to know what must be within the range, like "Your age must be over 0 and less than 110".
One solution is to use the name of the input in the error message. This forces the user to name inputs in human readable form, which might not be possible. For example, I have an input for monthly overhead and I name it monthlyOverhead so that it is a legal variable name. You can't have a message that reads, "monthlyOverhead must be greater than 0". This just won't fly in a production environment. It needs to be nice and human readable and say, "Your monthly overhead must be greater than zero." However, you can't name your UIComponent "Your monthly overhead" especially if you intend to do JavaScript on the page. Besides, it's just bad style.
Another solution is requiring specific sub-classes for each message required. This clutters up the packages with tons of Validators and also requires too much coding. Yet another solution is an attribute to the JSP tag to tell the JSF implementation the key of the error message in the ResourceBundle. For example:
<jsf:text id="age" errorMessage="ageError"/>
This does not provide the ability to use parameterized messages (java.text.MessageFormat parameters that is) without further bogging down JSP pages with the value for each parameter. Let us assume that there is a message ResourceBundle that has an entry like, "ageError=Your age must be greater than {0} and less that {1}". The JSP tag would now look like this:
<jsf:text id="age" errorMessage="ageError" params="0,110"/>
Also, neither or these solutions take into consideration internationalization.
Since a large portion of any application is really the view and interaction, of which a large chunk is error messages, this is a major issue that must be considered. Also, we all know that once the CEO plays around with the application you'll get requests like, "I really wish this error message read this
" and then you're in for some major headaches, unless this problem is solved up front.
I however, do not see any good way of accomplishing this in the JSF architecture. Reusable Validators within JSF do not allow for much flexibility and it will more likely be the case that all validation will happen elsewhere so that full control over error messages can be achieved.
>> Page 6 (Internationalization)
<< Back to Programming
|