The main purpose of conversion and validation is to ensure values have been properly sanitized before updating model data. Subsequently, when the time comes to invoke application methods to actually do something with that data, you can safely make certain assumptions about the state of your model. Conversion and validation allow you to focus on business logic rather than the tedious qualifications of input data such as null checks, length qualifiers, range boundaries, etc.
It makes sense, then, that conversion and validation processes happen before component data is bound to your backing bean model in the update model data lifecycle phase. As you saw in Figure 1, conversion occurs in the apply request values phase and validation in the process validations phase. These phases are highlighted in Figure 2.
Figure 2. Conversion and validation phases of interest
Note that the conversion and validation processes outlined in Figure 2 represent the application flow when the UIInput
component's immediate
attribute is set to false
. Were the attribute set to true
, conversion and validation would occur earlier in the lifecycle, during the apply request values phase (see Figure 3). A detailed discussion about using the immediate attribute is beyond the scope of this article, but there are instances where it is useful -- such as managing dynamic lists (which you may recall from the previous article in this series) and even bypassing validation altogether (when used with a UICommand
component). Can you think of an application where it would be useful to bypass validation completely?
Figure 3 shows where conversion and validation would occur in the JSF application lifecycle were the immediate
attribute set to true
.
Figure 3. When the immediate attribute is true
Basically, in any scenario where a user must enter data, that data must be validated. If the overall data entry is optional, however, then validation need not occur. One way of working around the validation phase of the JSF lifecycle is to utilize the UICommand
component's immediate
attribute, which can force the action to be invoked during the apply request values phase before the process validations phase (rather than during the invoke application phase, which occurs after the process validations phase).
The immediate
attribute allows you to control page flow through standard navigation rules while bypassing validation altogether. You could implement this technique for specific scenarios such as online wizards with optional steps and/or forms (such as when the user clicks the Skip button to advance to the next view) or in cases where the user cancels out a form for some reason.