Validation

There are several ways to validate the content of a component. In general input components have the option for validation. In the following sections, the different kinds von validation are explained.

Tag Library Documentation: <tc:sheet/> | <tc:column/>

Required

A simple way of validation is the required attribute in some input components. The text area must not empty if the submit button is pressed. Otherwise a error message is displayed.

<tc:textarea label="Text Area" required="true"/>

Validate Length

The length of a value can be validated by the <f:validateLength/> tag. Length means the number of characters. In this example, the given string must have at least two characters and maximal four characters.

Please note, an empty string will not be validated.

<tc:in label="Input">
  <f:validateLength minimum="2" maximum="4"/>
</tc:in>

Validate Range

The range of a number can be validated via <f:validateLongRange/> or - for floating point numbers - <f:validateDoubleRange/>. In the following inputfield numbers from 3 to 77 can be added.

<tc:in label="Number" markup="number">
  <f:validateLongRange minimum="3" maximum="77"/>
</tc:in>

Regex Validation

A value can be validated against a regex patter via the <f:validateRegex/> tag.

<tc:in label="Letter & Number" >
  <f:validateRegex pattern="[A-Za-z][0-9]"/>
</tc:in>

The value in the field must be a word character combined with a number. For example 'T3'.

Custom Validator

It's also possible to add custom validators using the validator attribute of an input component. In this case, only the string 'Tobago' (uppercase/lowercase ignored) is accepted.

Two Field Validation

A validator check the password against the confirmation field. To get access to the confirmation field from the validator a binding with the binding attribute and the <f:attribute/> tag is created.

<tc:in label="Password" value="qwe" validator="#{validationController.passwordValidator}">
  <f:attribute name="confirmationField" value="#{confirmBinding}"/>
</tc:in>
<tc:in label="Confirmation" value="asdf" binding="#{confirmBinding}"/>
<?xml version="1.0" encoding="UTF-8"?> <!-- * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. --> <ui:composition template="/main.xhtml" xmlns="http://www.w3.org/1999/xhtml" xmlns:tc="http://myfaces.apache.org/tobago/component" xmlns:ui="http://java.sun.com/jsf/facelets" xmlns:f="http://java.sun.com/jsf/core"> <ui:param name="title" value="#{demoBundle.validation}"/> <p>There are several ways to validate the content of a component. In general input components have the option for validation. In the following sections, the different kinds von validation are explained.</p> <p>Tag Library Documentation: <tc:link label="&lt;tc:sheet/>" image="#{request.contextPath}/image/feather-leaf.png" link="#{demoBundle.tagDocUrl}/#{apiController.currentRelease}/tld/tc/sheet.html"/> | <tc:link label="&lt;tc:column/>" image="#{request.contextPath}/image/feather-leaf.png" link="#{demoBundle.tagDocUrl}/#{apiController.currentRelease}/tld/tc/column.html"/></p> <tc:section label="Required"> <p>A simple way of validation is the <code>required</code> attribute in some input components. The text area must not empty if the submit button is pressed. Otherwise a error message is displayed.</p> <pre><code class="language-markup">&lt;tc:textarea label="Text Area" required="true"/></code></pre> <tc:form id="required"> <tc:textarea id="textarea" label="Text Area" required="true"/> <tc:button id="submit_r" label="Submit"/> </tc:form> </tc:section> <tc:section label="Validate Length"> <p>The length of a value can be validated by the <code class="language-markup">&lt;f:validateLength/></code> tag. Length means the number of characters. In this example, the given string must have at least two characters and maximal four characters.</p> <p>Please note, an empty string will not be validated.</p> <pre><code class="language-markup">&lt;tc:in label="Input"> &lt;f:validateLength minimum="2" maximum="4"/> &lt;/tc:in></code></pre> <tc:form id="validateLength"> <tc:in id="in_vl" label="Text"> <f:validateLength minimum="2" maximum="4"/> </tc:in> <tc:button id="submit_vl" label="Submit"/> </tc:form> </tc:section> <tc:section label="Validate Range"> <p>The range of a number can be validated via <code class="language-markup">&lt;f:validateLongRange/></code> or - for floating point numbers - <code class="language-markup">&lt;f:validateDoubleRange/></code>. In the following inputfield numbers from 3 to 77 can be added.</p> <pre><code class="language-markup">&lt;tc:in label="Number" markup="number"> &lt;f:validateLongRange minimum="3" maximum="77"/> &lt;/tc:in></code></pre> <tc:form id="validateRange"> <tc:in id="in_vr" label="Number" markup="number"> <f:validateLongRange minimum="3" maximum="77"/> </tc:in> <tc:button id="submit_vr" label="Submit"/> </tc:form> </tc:section> <tc:section label="Regex Validation"> <p>A value can be validated against a regex patter via the <code class="language-markup">&lt;f:validateRegex/></code> tag.</p> <pre><code class="language-markup">&lt;tc:in label="Letter &amp; Number" > &lt;f:validateRegex pattern="[A-Za-z][0-9]"/> &lt;/tc:in></code></pre> <tc:form id="regexValidation"> <p>The value in the field must be a word character combined with a number. For example 'T3'.</p> <tc:in id="in_rv" label="Letter &amp; Number" value="#{validationController.letter}"> <f:validateRegex pattern="[A-Za-z][0-9]"/> </tc:in> <tc:button id="submit_rv" label="Submit"/> </tc:form> </tc:section> <tc:section label="Custom Validator"> <p>It's also possible to add custom validators using the <code>validator</code> attribute of an input component. In this case, only the string 'Tobago' (uppercase/lowercase ignored) is accepted.</p> <tc:form id="customValidator"> <tc:in id="in_cv" label="Text" validator="#{validationController.customValidator}"/> <tc:button id="submit_cv" label="Submit"/> </tc:form> </tc:section> <tc:section label="Two Field Validation"> <p>A validator check the password against the confirmation field. To get access to the confirmation field from the validator a binding with the <code>binding</code> attribute and the <code class="language-markup">&lt;f:attribute/></code> tag is created.</p> <pre><code class="language-markup">&lt;tc:in label="Password" value="qwe" validator="\#{validationController.passwordValidator}"> &lt;f:attribute name="confirmationField" value="\#{confirmBinding}"/> &lt;/tc:in> &lt;tc:in label="Confirmation" value="asdf" binding="\#{confirmBinding}"/></code></pre> <tc:form> <tc:in label="Password" required="true" validator="#{validationController.passwordValidator}"> <f:attribute name="confirmationField" value="#{confirmBinding}"/> </tc:in> <tc:in label="Confirmation" required="true" binding="#{confirmBinding}"/> <tc:button label="Submit"/> </tc:form> </tc:section> </ui:composition>