8.2. <rich:graphValidator> object validation
The
<rich:graphValidator> component is used to wrap a set of input components related to one object. The object defined by the <rich:graphValidator> component can then be completely validated. The validation includes all object properties, even those which are not bound to the individual form components. Validation performed in this way allows for cross-field validation in complex forms.
Note
The
<rich:graphValidator> component performs a clone() method on the referenced bean instance during the validation phase. The cloned object is validated and triggers any required validation messages. As such, the model object remains clean, and the lifecycle is interrupted properly after the Process Validations phase.
Ensure the referenced object implements the
Cloneable interface, and allows a deep clone if required.
8.2.1. Basic usage
The
<rich:graphValidator> element must wrap all the input controls that are required to validate the object. The value attribute names the bean for the validating object.

Example 8.5. Basic usage
The example demonstrates a simple form for changing a password. The two entered passwords must match, so a
<rich:graphValidator> component is used for cross-field validation.
<h:form> <rich:graphValidator value="#{userBean}"> <rich:panel header="Change password"> <rich:messages/> <h:panelGrid columns="3"> <h:outputText value="Enter new password:" /> <h:inputSecret value="#{userBean.password}" id="pass"/> <rich:message for="pass"/> <h:outputText value="Confirm the new password:" /> <h:inputSecret value="#{userBean.confirm}" id="conf"/> <rich:message for="conf"/> </h:panelGrid> <a4j:commandButton value="Store changes" action="#{userBean.storeNewPassword}" /> </rich:panel> </rich:graphValidator> </h:form>
The input controls validate against the following bean:
@ManagedBean @RequestScoped public class UserBean implements Cloneable { @Size(min = 5, max = 15, message="Wrong size for password") private String password; @Size(min = 5, max = 15, message="Wrong size for confirmation") private String confirm; private String status = ""; @AssertTrue(message = "Different passwords entered!") public boolean isPasswordsEquals() { return password.equals(confirm); } public void storeNewPassword() { FacesContext.getCurrentInstance().addMessage("", new FacesMessage(FacesMessage.SEVERITY_INFO, "Succesfully changed!", "Succesfully changed!")); } ... }
When validation occurs, the whole object is validated against the annotation contstraints. The
@AssertTrue annotation relies on the isPasswordsEqual() function to check whether the two entered passwords are equal.
If the entered passwords do not match, an error message is displayed:

8.2.2. Reference data
component-type:org.richfaces.GraphValidatorcomponent-class:org.richfaces.component.UIGraphValidatorcomponent-family:org.richfaces.GraphValidatorhandler-class:org.richfaces.view.facelets.html.GraphValidatorHandler