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/>
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.
required
<tc:textarea label="Text Area" required="true"/>
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.
<f:validateLength/>
Please note, an empty string will not be validated.
<tc:in label="Input"> <f:validateLength minimum="2" maximum="4"/> </tc:in>
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.
<f:validateLongRange/>
<f:validateDoubleRange/>
<tc:in label="Number" markup="number"> <f:validateLongRange minimum="3" maximum="77"/> </tc:in>
A value can be validated against a regex patter via the <f:validateRegex/> tag.
<f:validateRegex/>
<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'.
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.
validator
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.
binding
<f:attribute/>
<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="<tc:sheet/>" image="#{request.contextPath}/image/feather-leaf.png" link="#{demoBundle.tagDocUrl}/#{apiController.currentRelease}/tld/tc/sheet.html"/> | <tc:link label="<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"><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"><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"><tc:in label="Input"> <f:validateLength minimum="2" maximum="4"/> </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"><f:validateLongRange/></code> or - for floating point numbers - <code class="language-markup"><f:validateDoubleRange/></code>. In the following inputfield numbers from 3 to 77 can be added.</p> <pre><code class="language-markup"><tc:in label="Number" markup="number"> <f:validateLongRange minimum="3" maximum="77"/> </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"><f:validateRegex/></code> tag.</p> <pre><code class="language-markup"><tc:in label="Letter & Number" > <f:validateRegex pattern="[A-Za-z][0-9]"/> </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 & 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"><f:attribute/></code> tag is created.</p> <pre><code class="language-markup"><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}"/></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>