JSF : Validators

JavaServer Faces technology supports a mechanism for validating the data of editable components. Validation is 3rd phase in life cycle. Each component in JavaServer Faces, created Error Messages during the life cycle of JSF and attached them to FacesContext object. Hence each message is attached to a component in the component tree and the message is displayed into the view at the end of JSF life cycle.

JSF supports both Declarative and Imperative means of validations

1.Declarative

Using JSF standard validators

Using Bean validation — JSR 303

2. Imperative

Validation Method in backing bean

Custom validator by implementing javax.faces.validator.Validator and annotating the class with @FacesValidator or validator tag in faces-config.xml

SF provides different tags to handle and display messages on the view. There are two message tags in Sun’s reference implementation of JSF HTML library:

<h:message />

<h:messages />

h:message is used to display message attached to one component. An attibute for=”” can be used to specify the id of a component whose error messages we need to display. h:message is used to display error message next to the component that generated the error. If more then one message is attached to that component, h:message will display the last message.

h:messages is used to display all messages at once, also the ones which are already displayed in a h:message in the page. You can place h:messages tag in start of your form. You may need to display only global messages using h:messages tag. When the value of the globalOnly attribute is true, only global messages (i.e. messages without a client Id) will be displayed; when the value is false, global messages will be displayed in addition to other messages that are already queued. By default globalOnly is false.

Each message can have a summary description and a detailed description. When using the h:message tag, the default is to show the detail message. When using the h:messages tag, the default is to display the summary descriptions. To change the defaults, modify the booleanshowSummary and showDetail attributes.

The above code will check for valid values of the “name” and “age” field. Notice the ‘label’ attribute in the h:inputText. This is what jsf will take while displaying the error message for a componenet id. If not mentioned , the defalut label will be formid+componentid, which may be not user friendly. Other attributes like required=true and <f:validateLongRange> are self explanatory. There are many such tags. In order to customize your messages, you need to add a message bundle element to your faces-config.xml.

javax.faces.validator.LongRangeValidator.MAXIMUM={1}: Value is greater than allowable maximum of ''{0}''
javax.faces.validator.LongRangeValidator.MINIMUM={1}: Value is less than allowable minimum of ''{0}''
javax.faces.validator.LengthValidator.MAXIMUM={1}: Value is greater than allowable maximum of ''{0}''
javax.faces.validator.LengthValidator.MINIMUM={1}: Value is less than allowable minimum of ''{0}''
javax.faces.component.UIInput.REQUIRED={0} is a mandatory field
javax.faces.validator.LongRangeValidator.NOT_IN_RANGE={2}: Field needs to be between the expected values of {0} and {1}

Till now we have seen Declarative Validation using JSF tags. Lets now see Declarative Validation using Bean Validation.

The Bean Validation JSR (JSR-303) defines a generic, tier-independent mechanism for specifying data validation constraints. The specification includes several standard constraint annotations (eg. @NotNull, @Size, @Min, @Max,@Pattern etc…) and also allows custom constraints to be defined. JSF 2 provides built-in integration with JSR-303 constraints. When you are using bean validation in your application, JSF automatically uses the constraints for beans that are referenced by UIInput values.

By default, the Bean Validator is enabled, therefore our JSF pages do not need to contain any info about the Bean Validator. When validation constraints fail, any associated error messages are automatically translated into FacesMessages by the JSF implementation.You can however disable the validator for any field of the ManagedBean

Validation using Backing Bean methods

A backing bean method can be used for doing validation of any input field. First we need to create the backing bean method that will get called during validation process. The signature of the method has to be like the validate method we overrrided previously.