Searching...
Wednesday, 14 August 2013

Skipping JSF Validation when required

13:22
Some cases we want to skip jsf validation for some components, here is one scenario

I have two text fields in a jsf page (Name and Address) and i have two submit buttons (Name Submit and Address Submit) . The requirement is when clicking on the Name Submit the Address empty Validation should be skipped ie Address validation will happens only when clicking the Address Submit. See below example to resolve this issue.

 

validationtest.xhtml


<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
     xmlns:h="http://java.sun.com/jsf/html">
    <h:head>
        <title>Facelet Title</title>
    </h:head>
    <h:body>
        <h2>JSF Validator Example</h2>
        <h:form id="validatorForm">
            <h:outputText id="name" value="Name :"/>
            <h:inputText id="nametext" value="#{newJSFManagedBean.name}" required="true">
            </h:inputText>
            <h:outputText id="address" value="Address :"/>
            <h:inputText id="addresstext" value="#{newJSFManagedBean.address}" required="#{!empty param['validatorForm:addresssubmit']}">
            </h:inputText>
            <h:commandButton value="Name Submission" id="namesubmit" action="#{newJSFManagedBean.nameSubmission}">
            </h:commandButton>
            <h:commandButton value="Address Submission" id="addresssubmit" action="#{newJSFManagedBean.addressSubmission}">
            </h:commandButton>
        </h:form>    </h:body>
</html>
In the above code Address field is validating only by click on address submit

Managed Bean

 

package com.vinod.jsf;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;

@ManagedBean
@RequestScoped
public class NewJSFManagedBean {
    private String name;
    private String address;

    public NewJSFManagedBean() {
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String nameSubmission() {
        return "validationtest.xhtml";
    }

    public String addressSubmission() {
        return "validationtest.xhtml";
    }
}

Deploy application and run it

1. Click on Name Submission (Validating only name)

 

sourceId=validatorForm:nametext[severity=(ERROR 2), summary=(validatorForm:nametext: Validation Error: Value is required.), detail=(validatorForm:nametext: Validation Error: Value is required.)]

2. Click on Address Submission (Validating name add address)


sourceId=validatorForm:nametext[severity=(ERROR 2), summary=(validatorForm:nametext: Validation Error: Value is required.), detail=(validatorForm:nametext: Validation Error: Value is required.)]

sourceId=validatorForm:addresstext[severity=(ERROR 2), summary=(validatorForm:addresstext: Validation Error: Value is required.), detail=(validatorForm:addresstext: Validation Error: Value is required.)]

Done..download application ..use mvn jetty:run to run the application

0 comments: