i need to know how to use m multiple validators for multiple fields like name and email id,and age and phone numbers. i have a clue that in ajax i can use multiple validaors for one form with different fields, but in jsf only one field like name validator or email validator is working, if i use two validators for the same form its not working and here is my code
package com.dnb;
import java.util.Date;
import javax.faces.component.UIComponent;
public class UserBean
{
private String username;
public String getUsername()
{
return username;
}
public void setUserName(String UserName) {
this.username = UserName;
}
private String emailid;
public String getEmailId()
{
return emailid;
}
public void setEmailId(String EmailId) {
this.emailid = EmailId;
}
private int age;
private Object city;
public String getage()
{
return "";
}
public void setAge(int Age) {
this.age = age;
}
public String getcity()
{
return "";
}
public void setcity(int City) {
this.city = city;
}
public void success() {
setProgressString(getProgressString() + "(Strored successfully)");
}
private void setProgressString(String string) {
// TODO Auto-generated method stub
}
public String getProgressString() {
return getProgressString();
}
private boolean buttonRendered = true;
private boolean enabled=false;
private Long startTime;
public String startProcess() {
setEnabled(true);
setButtonRendered(false);
setStartTime(new Date().getTime());
return null;
}
public Long getCurrentValue(){
if (isEnabled()){
Long current = (new Date().getTime() - startTime)/1000;
if (current>100){
setButtonRendered(true);
}else if (current.equals(0)){
return new Long(1);
}
return (new Date().getTime() - startTime)/1000;
} if (startTime == null) {
return Long.valueOf(-1);
}
else
return Long.valueOf(101);
}
public boolean isEnabled() {
return enabled;
}
public void setEnabled(boolean enabled) {
this.enabled = enabled;
}
public Long getStartTime() {
return startTime;
}
public void setStartTime(Long startTime) {
this.startTime = startTime;
}
public boolean isButtonRendered() {
return buttonRendered;
}
public void setButtonRendered(boolean buttonRendered) {
this.buttonRendered = buttonRendered;
}
}
JSF :
<%@ taglib uri="http://richfaces.org/a4j" prefix="a4j"%>
<%@ taglib uri="http://richfaces.org/rich" prefix="rich"%>
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h"%>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f"%>
<html>
<body>
<f:view>
<h:form id="ajaxValidatorForm">
<rich:panel>
<f:facet name="header">
<h:outputText value="User Info:" />
</f:facet>
<h:panelGrid columns="4">
<h:outputText value="Name:" />
<h:inputText value="#{userBean.username}" id="username" required="true" >
<f:validator validatorId="nameValidator" />
<h:message for="username"></h:message>
</h:inputText>
<h:outputText value="Email:" />
<h:inputText value="#{userBean.emailId}" id="emailid" required="true" >
<f:validator validatorId="nameValidator" />
<h:message for="emailid"></h:message>
</h:inputText>
</h:panelGrid>
</rich:panel>
</h:form>
</f:view>
</body>
</html>
name validator:
package com.dnb;
import javax.faces.application.FacesMessage;
public class NameValidator implements Validator
{
public void validate(FacesContext context, UIComponent component, Object value)
throws ValidatorException
{
String name = (String) value;
if(!name.contains("123"))
{
FacesMessage message = new FacesMessage();
message.setSeverity(FacesMessage.SEVERITY_ERROR);
message.setSummary("Name must be 3 to 8 letters");
message.setDetail("Name must be 3 to 8 letters");
context.addMessage("userForm:Name", message);
throw new ValidatorException(message);
}
}
Email validator:
package com.dnb;
import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.validator.Validator;
import javax.faces.validator.ValidatorException;
public class EmailValidator implements Validator
{
public void validate(FacesContext context, UIComponent component, Object value)
throws ValidatorException
{
String email = (String) value;
if(!email.contains("@"))
{
FacesMessage message = new FacesMessage();
message.setSeverity(FacesMessage.SEVERITY_ERROR);
message.setSummary("Email is not valid,[email protected]");
message.setDetail("Email is not valid,[email protected]");
context.addMessage("userForm:Email", message);
throw new ValidatorException(message);
}
}
}
pls provide some perfect answer for my question.
If i use one field as E.mail i am geeting validator message, if i use two or more fields like name, phone number i m not getting validator messages.only one field is working,how to use multiple fields in jsf validators