Class BeanValidator
- java.lang.Object
-
- jakarta.faces.validator.BeanValidator
-
- All Implemented Interfaces:
PartialStateHolder,StateHolder,Validator,EventListener
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 Description static StringDISABLE_DEFAULT_BEAN_VALIDATOR_PARAM_NAMEIf this param is defined, and callingtoLowerCase().equals(“true”)on aStringrepresentation of its value returnstrue, the runtime must not automatically add the validator with validator-id equal to the value of the symbolic constantVALIDATOR_IDto the list of default validators.static StringEMPTY_VALIDATION_GROUPS_PATTERNThe regular expression pattern that identifies an empty list of validation groups.static StringENABLE_VALIDATE_WHOLE_BEAN_PARAM_NAMEIf this param is set, and calling toLowerCase().equals("true") on a String representation of its value returnstruetake the additional actions relating to<validateWholeBean />specified invalidate(jakarta.faces.context.FacesContext, jakarta.faces.component.UIComponent, java.lang.Object).static StringMESSAGE_IDThe message identifier of theFacesMessageto be created if a constraint failure is found.static StringVALIDATION_GROUPS_DELIMITERThe delimiter that is used to separate the list of fully-qualified group names as strings.static StringVALIDATOR_FACTORY_KEYThe name of the Jakarta Servlet context attribute which holds the object used by Jakarta Faces to obtain Validator instances.static StringVALIDATOR_IDThe standard validator id for this validator, as defined by the Jakarta Server Face specification.
-
Constructor Summary
Constructors Constructor Description BeanValidator()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclearInitialState()Reset the PartialStateHolder to a non-delta tracking state.StringgetValidationGroups()Return the validation groups passed to the Validation API when checking constraints.booleaninitialStateMarked()Returntrueif delta state changes are being tracked, otherwisefalsebooleanisTransient()If true, the Object implementing this interface must not participate in state saving or restoring.voidmarkInitialState()The runtime must ensure that thePartialStateHolder.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.voidrestoreState(FacesContext context, Object state)Perform any processing required to restore the state from the entries in the state Object.ObjectsaveState(FacesContext context)Gets the state of the instance as aSerializableObject.voidsetTransient(boolean transientValue)Denotes whether or not the Object implementing this interface must or must not participate in state saving or restoring.voidsetValidationGroups(String validationGroups)A comma-separated list of validation groups which are used to filter which validations get checked by this validator.voidvalidate(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 Jakarta Server Face specification.
- See Also:
- Constant Field Values
-
MESSAGE_ID
public static final String MESSAGE_ID
The message identifier of the
FacesMessageto 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 aStringwhose 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 Jakarta Server Face 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 Jakarta Servlet context attribute which holds the object used by Jakarta Faces to obtain Validator instances. If the Jakarta Servlet context attribute is missing or contains a null value, Jakarta Server Faces is free to use this Jakarta 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 aStringrepresentation of its value returnstrue, the runtime must not automatically add the validator with validator-id equal to the value of the symbolic constantVALIDATOR_IDto the list of default validators. Setting this parameter totruewill 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
-
ENABLE_VALIDATE_WHOLE_BEAN_PARAM_NAME
public static final String ENABLE_VALIDATE_WHOLE_BEAN_PARAM_NAME
If this param is set, and calling toLowerCase().equals("true") on a String representation of its value returns
truetake the additional actions relating to<validateWholeBean />specified invalidate(jakarta.faces.context.FacesContext, jakarta.faces.component.UIComponent, java.lang.Object).- Since:
- 2.3
- 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
Defaultgroup 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
Defaultgroup will be used.- Returns:
- the value of the
validatinGroupsattribute.
-
validate
public void validate(FacesContext context, UIComponent component, Object value)
Verify that the value is valid according to the Bean Validation constraints.
Obtain a
ValidatorFactoryinstance 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 ofDefaultas 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 Jakarta Expression Language, obtain theValueReferencefrom valueExpression and let valueBaseClase be the return from callingValueReference.getBase()and valueProperty be the return from callingValueReference.getProperty(). If an earlier version of Jakarta Expression Language is present, use the appropriate methods to inspect valueExpression and derive values for valueBaseClass and valueProperty.If no
ValueReferencecan be obtained, take no action and return.If
ValueReference.getBase()returnnull, take no action and return.Obtain the
ValidatorContextfrom theValidatorFactory.Decorate the
MessageInterpolatorreturned fromValidatorFactory.getMessageInterpolator()with one that leverages theLocalereturned fromUIViewRoot.getLocale(), and store it in theValidatorContextusingValidatorContext.messageInterpolator(jakarta.validation.MessageInterpolator).Obtain the
Validatorinstance from thevalidatorContext.Obtain a
jakarta.validation.BeanDescriptorfrom thejakarta.validation.Validator. IfhasConstraints()on theBeanDescriptorreturns 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 aFacesMessagewhere the summary and detail are the return from callingConstraintViolation.getMessage(). Capture all suchFacesMessageinstances into aCollectionand pass them toValidatorException(java.util.Collection). If theENABLE_VALIDATE_WHOLE_BEAN_PARAM_NAMEapplication parameter is enabled and thisValidatorinstance has validation groups other than or in addition to theDefaultgroup, record the fact that this field failed validation so that any<f:validateWholeBean />component later in the tree is able to skip class-level validation for the bean for which this particular field is a property. Regardless of whether or notENABLE_VALIDATE_WHOLE_BEAN_PARAM_NAMEis set, throw the new exception.If the returned
Setis empty, theENABLE_VALIDATE_WHOLE_BEAN_PARAM_NAMEapplication parameter is enabled and thisValidatorinstance has validation groups other than or in addition to theDefaultgroup, record the fact that this field passed validation so that any<f:validateWholeBean />component later in the tree is able to allow class-level validation for the bean for which this particular field is a property.- Specified by:
validatein 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:StateHolderGets the state of the instance as a
SerializableObject.If the class that implements this interface has references to instances that implement StateHolder (such as a
UIComponentwith event handlers, validators, etc.) this method must call theStateHolder.saveState(jakarta.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 theStateManagerThis method must not alter the state of the implementing object. In other words, after executing this code:
Object state = component.saveState(facesContext);componentshould be the same as before executing it.The return from this method must be
Serializable- Specified by:
saveStatein interfaceStateHolder- Parameters:
context- the Faces context.- Returns:
- the saved state.
-
restoreState
public void restoreState(FacesContext context, Object state)
Description copied from interface:StateHolderPerform 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
UIComponentwith event handlers, validators, etc.) this method must call theStateHolder.restoreState(jakarta.faces.context.FacesContext, java.lang.Object)method on all those instances as well.If the
stateargument isnull, take no action and return.- Specified by:
restoreStatein interfaceStateHolder- Parameters:
context- the Faces context.state- the state.
-
markInitialState
public void markInitialState()
Description copied from interface:PartialStateHolderThe 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()returnstruefrom the timemarkInitialState()is called untilPartialStateHolder.clearInitialState()is called, after which timeinitialStateMarked()must returnfalse. Also, during the time that the instance returnstruefrominitialStateMarked(), the implementation must return only the state that has changed in its implementation ofStateHolder.saveState(jakarta.faces.context.FacesContext).- Specified by:
markInitialStatein interfacePartialStateHolder
-
initialStateMarked
public boolean initialStateMarked()
Description copied from interface:PartialStateHolderReturn
trueif delta state changes are being tracked, otherwisefalse- Specified by:
initialStateMarkedin interfacePartialStateHolder- Returns:
trueif the initial state is marked,falseotherwise.
-
clearInitialState
public void clearInitialState()
Description copied from interface:PartialStateHolderReset the PartialStateHolder to a non-delta tracking state.
- Specified by:
clearInitialStatein interfacePartialStateHolder
-
isTransient
public boolean isTransient()
Description copied from interface:StateHolderIf true, the Object implementing this interface must not participate in state saving or restoring.
- Specified by:
isTransientin interfaceStateHolder- Returns:
trueif transient,falseotherwise.
-
setTransient
public void setTransient(boolean transientValue)
Description copied from interface:StateHolderDenotes whether or not the Object implementing this interface must or must not participate in state saving or restoring.
- Specified by:
setTransientin interfaceStateHolder- Parameters:
transientValue- boolean passtrueif this Object will not participate in state saving or restoring, otherwise passfalse.
-
-