## User Input Data Validation

Validation of user input is a very frequent situation for a developer. RichFaces library offers 2 componenta to get this job done: <rich:graphValidator>, and <rich:validator>. <rich:graphValidator> is intended to validate the whole object or the graph of interrelated objects and the validation occurs when the whole form is submitted. While <rich:validator> validates only one input field or a value at a time, validation is activated upon some event and adds interactivity to the application. Both components use Hibernate validators which helps to locate validation logic in one place, such approach is really helpful given that usually data validation logic is stored in multiple places including UI pages and in Java code that interacts with a database.

Let's have a look at the components usage on the registration page. This is how the page looks like (some irrelevant details were removed from the example):

                <rich:graphValidator>
                        …
                    <h:inputText id="userLoginName" value="#{'#{'}authenticator.user.login}" styleClass="reg-table-input" />
                    <rich:messages for="userLoginName" styleClass="required" />

                    <h:inputSecret required="true" id="userPassword" value="#{'#{'}authenticator.user.password}"
                        styleClass="reg-table-input" />
                    <rich:messages for="userPassword" styleClass="required" />

                    <h:inputSecret required="true" id="userConfirmPassword"
                        value="#{'#{'}authenticator.user.confirmPassword}" styleClass="reg-table-input" />
                    <rich:messages for="userConfirmPassword" styleClass="required" />
                        …
                    <a4j:outputPanel id="userCalendar" layout="block">
                        <rich:calendar id="userBirthDate" enableManualInput="true" value="#{'#{'}authenticator.user.birthDate}"
                            cellWidth="24px" cellHeight="22px" inputClass="reg-table-input">
                            <rich:validator event="inputblur" />
                        </rich:calendar>
                    </a4j:outputPanel>
                    <rich:messages for="userBirthDate" styleClass="required" />
                        …
                </rich:graphValidator>

                <richx:commandButton actionListener="#{'#{'}authenticator.register(authenticator.user)}"
                    render="mainArea,treePanel,searchPanel,headerPanel" value="#{'#{'}messages['user.register']}" />
                <richx:commandButton actionListener="#{'#{'}controller.cancelRegistration()}" render="mainArea" immediate="true"
                    value="#{'#{'}messages['user.cancel']}" />

<rich:graphValidator> validates the entity User object, in which restrictions are set with the help of Hibernate annotations. When the Register button is clicked on the name, password, sex etc. fields are validated sequentially. In case of an error (for example, if a loginName contains only on character and the annotation restricts it to at least 3 characters to be typed in) a error message in red color is displayed next to the input field and the request is aborted. If all values are valid the `authenticator.register(authenticator.user)` method will be invoked and the user will be saved to the database.

<rich:validator> acts in a slightly different way, in our case it is attached to the `authenticator.user.birthDate` field. When the value of the field is changed and the field loses focus it is immediately validated. If the input data is incorrect and error message will displayed, which is a quick way to respond to user input errors and avoid sending incorrect data to the server.

If you would like to get more details about the validators that RichFaces library provides please visit Showcase web page and RichFaces Developer Guide.