- java.lang.Object
-
- javax.faces.validator.BeanValidator
-
- All Implemented Interfaces:
- EventListener, PartialStateHolder, StateHolder, Validator
public class BeanValidator extends Object implements Validator, PartialStateHolder
A Validator that delegates validation of the bean property to the Bean Validation API.
- Since:
- 2.0
-
-
Field Summary
Fields Modifier and Type Field and Description static String
DISABLE_DEFAULT_BEAN_VALIDATOR_PARAM_NAME
If this param is defined, and calling
toLowerCase().equals(“true”)
on aString
representation of its value returnstrue
, the runtime must not automatically add the validator with validator-id equal to the value of the symbolic constantVALIDATOR_ID
to the list of default validators.static String
EMPTY_VALIDATION_GROUPS_PATTERN
The regular expression pattern that identifies an empty list of validation groups.
static String
MESSAGE_ID
The message identifier of theFacesMessage
to be created if a constraint failure is found.static String
VALIDATION_GROUPS_DELIMITER
The delimiter that is used to separate the list of fully-qualified group names as strings.
static String
VALIDATOR_FACTORY_KEY
The name of the servlet context attribute which holds the object used by JSF to obtain Validator instances.
static String
VALIDATOR_ID
The standard validator id for this validator, as defined by the JSF specification.
-
Fields inherited from interface javax.faces.validator.Validator
NOT_IN_RANGE_MESSAGE_ID
-
-
Constructor Summary
Constructors Constructor and Description BeanValidator()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method and Description void
clearInitialState()
Reset the PartialStateHolder to a non-delta tracking state.
String
getValidationGroups()
Return the validation groups passed to the Validation API when checking constraints.
boolean
initialStateMarked()
Return
true
if delta state changes are being tracked, otherwisefalse
boolean
isTransient()
If true, the Object implementing this interface must not participate in state saving or restoring.void
markInitialState()
The runtime must ensure that the
PartialStateHolder.markInitialState()
method is called on each instance of this interface in the view at the appropriate time to indicate the component is in its initial state.void
restoreState(FacesContext context, Object state)
Perform any processing required to restore the state from the entries in the state Object.Object
saveState(FacesContext context)
Gets the state of the instance as aSerializable
Object.void
setTransient(boolean transientValue)
Denotes whether or not the Object implementing this interface must or must not participate in state saving or restoring.void
setValidationGroups(String validationGroups)
A comma-separated list of validation groups which are used to filter which validations get checked by this validator.
void
validate(FacesContext context, UIComponent component, Object value)
Verify that the value is valid according to the Bean Validation constraints.
-
-
-
Field Detail
-
VALIDATOR_ID
public static final String VALIDATOR_ID
The standard validator id for this validator, as defined by the JSF specification.
- See Also:
- Constant Field Values
-
MESSAGE_ID
public static final String MESSAGE_ID
The message identifier of the
FacesMessage
to be created if a constraint failure is found. The message format string for this message may optionally include the following placeholders:{0}
replaced by the interpolated message from Bean Validation.{1}
replaced by aString
whose value is the label of the input component that produced this message.
The message format string provided by the default implementation should be a the placeholder {0}, thus fully delegating the message handling to Bean Validation. A developer can override this message format string to make it conform to other JSF validator messages (i.e., by including the component label)
- See Also:
- Constant Field Values
-
VALIDATOR_FACTORY_KEY
public static final String VALIDATOR_FACTORY_KEY
The name of the servlet context attribute which holds the object used by JSF to obtain Validator instances. If the servlet context attribute is missing or contains a null value, JSF is free to use this servlet context attribute to store the ValidatorFactory bootstrapped by this validator.
- See Also:
- Constant Field Values
-
VALIDATION_GROUPS_DELIMITER
public static final String VALIDATION_GROUPS_DELIMITER
The delimiter that is used to separate the list of fully-qualified group names as strings.
- See Also:
- Constant Field Values
-
EMPTY_VALIDATION_GROUPS_PATTERN
public static final String EMPTY_VALIDATION_GROUPS_PATTERN
The regular expression pattern that identifies an empty list of validation groups.
- See Also:
- Constant Field Values
-
DISABLE_DEFAULT_BEAN_VALIDATOR_PARAM_NAME
public static final String DISABLE_DEFAULT_BEAN_VALIDATOR_PARAM_NAME
If this param is defined, and calling
toLowerCase().equals(“true”)
on aString
representation of its value returnstrue
, the runtime must not automatically add the validator with validator-id equal to the value of the symbolic constantVALIDATOR_ID
to the list of default validators. Setting this parameter totrue
will have the effect of disabling the automatic installation of Bean Validation to every input component in every view in the application, though manual installation is still possible.- See Also:
- Constant Field Values
-
-
Method Detail
-
setValidationGroups
public void setValidationGroups(String validationGroups)
A comma-separated list of validation groups which are used to filter which validations get checked by this validator. If the validationGroupsArray attribute is omitted or is empty, the validation groups will be inherited from the branch defaults or, if there are no branch defaults, the
Default
group will be used.- Parameters:
validationGroups
- comma-separated list of validation groups (string with only spaces and commas treated as null)
-
getValidationGroups
public String getValidationGroups()
Return the validation groups passed to the Validation API when checking constraints. If the validationGroupsArray attribute is omitted or empty, the validation groups will be inherited from the branch defaults, or if there are no branch defaults, the
Default
group will be used.
-
validate
public void validate(FacesContext context, UIComponent component, Object value)
Verify that the value is valid according to the Bean Validation constraints.
Obtain a
ValidatorFactory
instance by callingValidation.buildDefaultValidatorFactory()
.Let validationGroupsArray be a
Class []
representing validator groups set on the component by the tag handler for this validator. The first search component terminates the search for the validation groups value. If no such value is found use the class name ofDefault
as the value of the validation groups.Let valueExpression be the return from calling
UIComponent.getValueExpression(java.lang.String)
on the argument component, passing the literal string “value” (without the quotes) as an argument. If this application is running in an environment with a Unified EL Implementation for Java EE6 or later, obtain theValueReference
from valueExpression and let valueBaseClase be the return from callingValueReference.getBase()
and valueProperty be the return from callingValueReference.getProperty()
. If an earlier version of the Unified EL is present, use the appropriate methods to inspect valueExpression and derive values for valueBaseClass and valueProperty.If no
ValueReference
can be obtained, take no action and return.If
ValueReference.getBase()
returnnull
, take no action and return.Obtain the
ValidatorContext
from theValidatorFactory
.Decorate the
MessageInterpolator
returned fromValidatorFactory.getMessageInterpolator()
with one that leverages theLocale
returned fromUIViewRoot.getLocale()
, and store it in theValidatorContext
usingValidatorContext.messageInterpolator(javax.validation.MessageInterpolator)
.Obtain the
Validator
instance from thevalidatorContext
.Obtain a
javax.validation.BeanDescriptor
from thejavax.validation.Validator
. IfhasConstraints()
on theBeanDescriptor
returns false, take no action and return. Otherwise proceed.Call
Validator.validateValue(java.lang.Class<T>, java.lang.String, java.lang.Object, java.lang.Class<?>...)
, passing valueBaseClass, valueProperty, the value argument, and validatorGroupsArray as arguments.If the returned
Set<
is non-empty, for each element in theConstraintViolation
>Set
, create aFacesMessage
where the summary and detail are the return from callingConstraintViolation.getMessage()
. Capture all suchFacesMessage
instances into aCollection
and pass them toValidatorException.ValidatorException(java.util.Collection)
, throwing the new exception.- Specified by:
validate
in interfaceValidator
- Parameters:
context
- FacesContext for the request we are processingcomponent
- UIComponent we are checking for correctnessvalue
- the value to validate- Throws:
ValidatorException
- if validation fails
-
saveState
public Object saveState(FacesContext context)
Description copied from interface:StateHolder
Gets the state of the instance as a
Serializable
Object.If the class that implements this interface has references to instances that implement StateHolder (such as a
UIComponent
with event handlers, validators, etc.) this method must call theStateHolder.saveState(javax.faces.context.FacesContext)
method on all those instances as well. This method must not save the state of children and facets. That is done via theStateManager
This method must not alter the state of the implementing object. In other words, after executing this code:
Object state = component.saveState(facesContext);
component
should be the same as before executing it.The return from this method must be
Serializable
- Specified by:
saveState
in interfaceStateHolder
-
restoreState
public void restoreState(FacesContext context, Object state)
Description copied from interface:StateHolder
Perform any processing required to restore the state from the entries in the state Object.
If the class that implements this interface has references to instances that also implement StateHolder (such as a
UIComponent
with event handlers, validators, etc.) this method must call theStateHolder.restoreState(javax.faces.context.FacesContext, java.lang.Object)
method on all those instances as well.If the
state
argument isnull
, take no action and return.- Specified by:
restoreState
in interfaceStateHolder
-
markInitialState
public void markInitialState()
Description copied from interface:PartialStateHolder
The runtime must ensure that the
PartialStateHolder.markInitialState()
method is called on each instance of this interface in the view at the appropriate time to indicate the component is in its initial state. The implementor of the interface must ensure thatPartialStateHolder.initialStateMarked()
returnstrue
from the timemarkInitialState()
is called untilPartialStateHolder.clearInitialState()
is called, after which timeinitialStateMarked()
must returnfalse
. Also, during the time that the instance returnstrue
frominitialStateMarked()
, the implementation must return only the state that has changed in its implementation ofStateHolder.saveState(javax.faces.context.FacesContext)
.- Specified by:
markInitialState
in interfacePartialStateHolder
-
initialStateMarked
public boolean initialStateMarked()
Description copied from interface:PartialStateHolder
Return
true
if delta state changes are being tracked, otherwisefalse
- Specified by:
initialStateMarked
in interfacePartialStateHolder
-
clearInitialState
public void clearInitialState()
Description copied from interface:PartialStateHolder
Reset the PartialStateHolder to a non-delta tracking state.
- Specified by:
clearInitialState
in interfacePartialStateHolder
-
isTransient
public boolean isTransient()
Description copied from interface:StateHolder
If true, the Object implementing this interface must not participate in state saving or restoring.
- Specified by:
isTransient
in interfaceStateHolder
-
setTransient
public void setTransient(boolean transientValue)
Description copied from interface:StateHolder
Denotes whether or not the Object implementing this interface must or must not participate in state saving or restoring.
- Specified by:
setTransient
in interfaceStateHolder
- Parameters:
transientValue
- boolean passtrue
if this Object will not participate in state saving or restoring, otherwise passfalse
.
-
-
Traduction non disponible
Les API Java ne sont pas encore traduites en français sur l'infobrol. Seule la version anglaise est disponible pour l'instant.
Version en cache
21/08/2025 13:05:54 Cette version de la page est en cache (à la date du 21/08/2025 13:05:54) afin d'accélérer le traitement.Vous pouvez activer le mode utilisateur dans le menu en haut pour afficher la version plus récente de la page.
Document créé le 11/06/2005, dernière modification le 18/08/2025
Source du document imprimé : https://www.gaudry.be/java-api-javaee-rf-javax/faces/validator/BeanValidator.html
L'infobrol est un site personnel dont le contenu n'engage que moi. Le texte est mis à disposition sous licence CreativeCommons(BY-NC-SA). Plus d'info sur les conditions d'utilisation et sur l'auteur.
Références
Ces références et liens indiquent des documents consultés lors de la rédaction de cette page, ou qui peuvent apporter un complément d'information, mais les auteurs de ces sources ne peuvent être tenus responsables du contenu de cette page.
L'auteur de ce site est seul responsable de la manière dont sont présentés ici les différents concepts, et des libertés qui sont prises avec les ouvrages de référence. N'oubliez pas que vous devez croiser les informations de sources multiples afin de diminuer les risques d'erreurs.