Java Reference
In-Depth Information
requiredMessage="#{bundle.ReqCreditCard}" >
<f:converter converterId="ccno"/>
<f:validateRegex
pattern="\d{16}|\d{4} \d{4} \d{4} \d{4}|\d{4}-\d{4}-
\d{4}-\d{4}" />
</h:inputText>
• Bind the
Converter
implementation to a managed bean property using the
The JavaServer Faces implementation calls the converter's
getAsObject
method to
strip spaces and hyphens from the input value. The
getAsString
method is called
when the
bookcashier.xhtml
page is redisplayed; this happens if the user orders
more than $100 worth of books.
In the Duke's Tutoring case study, each converter is registered as the converter for a par-
ticular class. The converter is automatically invoked whenever that class is specified by a
value
attribute of an input component. In the following example, the
itemValue
at-
tribute (highlighted in bold) calls the converter for the
Guardian
class:
<h:selectManyListbox id="selectGuardiansMenu"
value="#{guardianManager.selectedGuardians}"
size="5">
<f:selectItems value="#{guardianManager.allGuardians}"
var="selectedGuardian"
itemLabel="#{selectedGuardian.name}"
itemValue="#{selectedGuardian}" />
</h:selectManyListbox>
Creating and Using a Custom Validator
If the standard validators or Bean Validation don't perform the validation checking you
need, you can create a custom validator to validate user input. As explained in
“
Validation
• Implement a managed bean method that performs the validation.
• Provide an implementation of the
javax.faces.validator.Validator
interface to perform the validation.
“Writing a Method to Perform Validation” in
The Java EE 6 Tutorial: Basic Concepts
ex-
plains how to implement a managed bean method to perform validation. The rest of this
section explains how to implement the
Validator
interface.