Show TOC Start of Content Area

Background documentation Validating User Input  Locate the document in its SAP Library structure

In JSF, you can easily implement checking the correctness (validating) of user input in Web forms. This is necessary because incorrect data can mess up your underlying business model. If you validate the user input before submitting the data, you can prevent any incorrect data entering your model.

Basically, your validation can affect the formatting, content and length of any user input. Usually, this applies to dates, numbers, currencies, and so on. You can use the default validators available in JSF or create custom ones.

In the following example, we have added validators for the Start Date and End Date fields in the Create Project page. If you do not supply correct dates, an error message is displayed:

This graphic is explained in the accompanying text

If you do not supply start or end date, another error message appears:

This graphic is explained in the accompanying text

The validators for the Start Date and End Date fields are described with the following source code in the prjCreate.jsp:

<h:outputText value="Start Date:" styleClass="ttw"/>

  <h:inputText id="startDate" value="#{projectList.newProject.startDate}"

        styleClass="inputc" required="true">

    <f:convertDateTime type="date" dateStyle="short"/>

  </h:inputText>

 

<h:outputText value="End Date:" styleClass="ttw"/>

<h:inputText id="endDate" value="#{projectList.newProject.endDate}"

        styleClass="inputc" required="true">

  <f:convertDateTime type="date" dateStyle="short"/>

</h:inputText>

. . .

<h:messages styleClass="error_msg"/>

This code extract states that the two fields are required, the date and time converter is used as a validator for each of them, and an error message is displayed if you try to supply invalid data or omit one of the fields.

The Project Name and Project Description fields are also validated according to their length. They are described in the following way:

<h:outputText value="Project Name:" styleClass="ttw"/>

<h:inputText id="prjName" value="#{projectList.newProject.project.title}"

        styleClass="inputc" required="true">

  <f:validateLength maximum="30"/>

</h:inputText>

 

<h:outputText value="Project Description:" styleClass="ttw"/>

<h:inputTextarea id="prjDescription"

                 value="#{projectList.newProject.project.description}"

        styleClass="inputc" required="true">

  <f:validateLength minimum="0" maximum="1000"/>

</h:inputTextarea>

. . .

<h:messages styleClass="error_msg"/>

This extract indicates that the two fields are required and the length validator is used for both of them. An error message is displayed if you try to supply a longer text for them or if you leave them blank.

Adding custom validators is not described in this tutorial.

End of Content Area