Form Validation using the JSF Validator Tags

This approach applies to sees if value passes rule of validator after checking required (requiredMessage) and type (converterMessage) and if not, stores error message and redisplays the form that use validatorMessage attribute to display the customized validator message.

Bean File
ProductBean2.java

package model;

import javax.faces.bean.ManagedBean;

@ManagedBean
public class ProductBean2 {

    private String pid, pname;
    private double price;
    private int qty;

    public String getPid() {
        return pid;
    }

    public void setPid(String pid) {
        this.pid = pid;
    }

    public String getPname() {
        return pname;
    }

    public void setPname(String pname) {
        this.pname = pname;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    public int getQty() {
        return qty;
    }

    public void setQty(int qty) {
        this.qty = qty;
    }

//Action Controller
    public String doPurchase() {

        //apply business logic
        return ("show-purchase2");//redirect to show-purchase2.xhtml

    }
}

productentry3.xhtml

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:h="http://java.sun.com/jsf/html">
    <h:head>
        <title>Product Entry</title>
        <link href="./css/default.css" 
              rel="stylesheet" type="text/css"/> 
    </h:head>
    <h:body>

        <f:view>
            <h:form>
                <h:panelGrid columns="3">
                    <h:outputLabel>Product ID:</h:outputLabel>
                    <h:inputText value="#{productBean2.pid}" required="true"
                                 requiredMessage="You must enter a Product ID"
                                 validatorMessage="Product ID must be 3 or 5 chars"
                                 id="productID">
                        <f:validateLength minimum="3" maximum="5"/>
                    </h:inputText>                    
                    <h:message for="productID" styleClass="error"/>
                    <h:outputLabel>Product Name:</h:outputLabel>
                    <h:inputText value="#{productBean2.pname}" required="true"
                                 requiredMessage="You must enter a Product Name"
                                 validatorMessage="Product Name must be at least 3 chars"
                                 id="productName">
                        <f:validateLength minimum="3"/>
                    </h:inputText>
                    <h:message for="productName" styleClass="error"/>
                    <h:outputLabel>Purchase Price:</h:outputLabel>
                    <h:inputText value="#{productBean2.price}" required="true"
                                 requiredMessage="You must enter an Purchase Price"
                                 converterMessage="Purchase Price must be a number"
                                 validatorMessage="Purchase Price must be 0.10 or greater"
                                 id="price">
                        <f:validateDoubleRange minimum="0.10"/>
                    </h:inputText>
                    <h:message for="price" styleClass="error"/>
                    <h:outputLabel>Quantity:</h:outputLabel>
                    <h:inputText value="#{productBean2.qty}" required="true"
                                 requiredMessage="You must enter a Quantity"
                                 converterMessage="Quantity must be a whole number"
                                 validatorMessage="Quantity must be 1 or more"
                                 id="qty">
                        <f:validateLongRange minimum="1"/>
                    </h:inputText>
                    <h:message for="qty" styleClass="error"/>

                </h:panelGrid>
                <h:commandButton value="Purchase"
                                 action="#{productBean2.doPurchase}"/>
            </h:form>
        </f:view>
    </h:body>
</html>

show-purchase2.xhtml

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:h="http://java.sun.com/jsf/html">
    <h:head>
        <title>Display Purchase</title>
    </h:head>
    <h:body>
        <f:view>
            <h:form>
                <h:outputLabel value="Purchase Info"/>
                <h:panelGrid columns="2">
                    <h:outputLabel>Product ID:</h:outputLabel>
                    <h:outputLabel value="#{productBean2.pid}"/>
                    <h:outputLabel>Product Name:</h:outputLabel>
                    <h:outputLabel value="#{productBean2.pname}"/>
                    <h:outputLabel>Purchase Price:</h:outputLabel>
                    <h:outputLabel value="#{productBean2.price}"/>
                    <h:outputLabel>Quantity:</h:outputLabel>
                    <h:outputLabel value="#{productBean2.qty}"/>                   
                </h:panelGrid>
            </h:form>
        </f:view>
    </h:body>
</html>

Screenshot:
jsf_validator_form_validation

 

admin

Hello! I am Md. Abdul Bari; founder and admin of javaknowledge; currently providing training as a J2EE faculty in The Computers Ltd. Dhaka, Bangladesh under an IT Scholarship project provided by IDB-BISEW. I am a self learner and passionate about training and writing. I am always trying my best to share my knowledge through my blog.

More Posts - Website

Follow Me:
TwitterFacebookGoogle Plus

3,210` Total Views

Leave a Reply

Your email address will not be published. Required fields are marked *


× 9 = sixty three