public class ValidateBean extends TagHandler
The <o:validateBean>
allows the developer to control bean validation on a per-UICommand
or UIInput
component basis, as well as validating a given bean at the class level.
The standard <f:validateBean>
only allows validation control on a per-form
or a per-request basis (by using multiple tags and conditional EL expressions in its attributes) which may end up in
boilerplate code.
The standard <f:validateBean>
also, despite its name, does not actually have any facilities to
validate a bean at all.
Some examples
Control bean validation per component
<h:commandButton value="submit" action="#{bean.submit}"> <o:validateBean validationGroups="javax.validation.groups.Default,com.example.MyGroup" /> </h:commandButton>
<h:selectOneMenu value="#{bean.selectedItem}"> <f:selectItems value="#{bean.availableItems}" /> <o:validateBean disabled="true" /> <f:ajax execute="@form" listener="#{bean.itemChanged}" render="@form" /> </h:selectOneMenu>
Validate a bean at the class level
<h:inputText value="#{bean.product.item}" /> <h:inputText value="#{bean.product.order}" /> <o:validateBean value="#{bean.product}" validationGroups="com.example.MyGroup" />
In order to validate a bean at the class level, all values from input components should first be actually set on that bean and only thereafter should the bean be validated. This however does not play well with the JSF approach where a model is only updated when validation passes. But for class level validation we seemingly can not validate until the model is updated. To break this tie, a copy of the model bean is made first, and then values are stored in this copy and validated there. If validation passes, the original bean is updated.
A bean is copied using the following strategies (in the order indicated):
Cloneable
interface and support cloning according to the rules of that interface. See CloneCopier
Serializable
interface and support serialization according to the rules of that interface. See SerializationCopier
CopyCtorCopier
NewInstanceCopier
If the above order is not ideal, or if an custom copy strategy is needed (e.g. when it's only needed to copy a few fields for the validation)
a strategy can be supplied explicitly via the copier
attribute. The value of this attribute can be any of the build-in copier implementations
given above, or can be a custom implementation of the Copier
interface.
BeanValidationEventListener
Modifier and Type | Class and Description |
---|---|
static class |
ValidateBean.CollectingValidator |
nextHandler, tag, tagId
Constructor and Description |
---|
ValidateBean(TagConfig config)
The tag constructor.
|
Modifier and Type | Method and Description |
---|---|
void |
apply(FaceletContext context,
UIComponent parent)
If the parent component has the
value attribute or is an instance of UICommand or
UIInput and is new and we're in the restore view phase of a postback, then delegate to
processValidateBean(UIComponent) . |
protected void |
processValidateBean(UIComponent component)
Check if the given component has participated in submitting the current form or action and if so, then perform
the bean validation depending on the attributes set.
|
getAttribute, getRequiredAttribute, toString
public ValidateBean(TagConfig config)
config
- The tag config.public void apply(FaceletContext context, UIComponent parent) throws IOException
value
attribute or is an instance of UICommand
or
UIInput
and is new and we're in the restore view phase of a postback, then delegate to
processValidateBean(UIComponent)
.IllegalArgumentException
- When the value
attribute is absent and the parent component is not
an instance of UICommand
or UIInput
.IOException
protected void processValidateBean(UIComponent component)
component
- The involved component.IllegalArgumentException
- When the parent form is missing.Copyright © 2012–2016 OmniFaces. All rights reserved.