Addons

HOWTO: Add a JSF Form Validation

Updated: March 18, 2024

The document is created and updated using the 'create' or 'update' view. You can check values on this document before it is saved in the repository.

To validate field, you add a JSF validator to the JSF component controlling the field.

If you're using layouts, you can add it as a property to the widget in edit mode.

Without layout:

<h:inputText type="text" id="userNo" value="#{NumberBean.guess}" validator="#{NumberBean.validate}"/>

Inside a layout:

<widget name="title" type="text">
  <translated>true</translated>
  <fields>
    <field>dc:title</field>
  </fields>
  <properties mode="edit">
    <property name="validator">#{myValidatorBean.validateTitle}</property>
  </properties>
</widget>

The method #{NumberBean.validate} method will be called during Process Validations phase to perform correctness checks on the values of this component. The expression must evaluate to a public method that takes FacesContext, UIComponent, and Object parameters, with a return type of void.

If you need to cross validate fields of a document (i.e.: makes sure dc:title and dc:description are different), refer to the the Cross validation with JSF blog entry.