aspose file tools*
The moose likes JSF and the fly likes JSF validation not validating Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSF
Bookmark "JSF validation not validating" Watch "JSF validation not validating" New topic
Author

JSF validation not validating

Kingsley Mullers
Ranch Hand

Joined: Jan 14, 2005
Posts: 48
Hi guys,

i have written a jsf validation class and it doesnt seem to be working,
my faces.config is configured like this. <validator>
<validator-id>validation</validator-id>
<validator-class>
za.co.fnb.pyramid.validations.Validation
</validator-class>
</validator>

and call my validator on jsf
<hutputText value="First name" />
<h:inputText id="Firstname" value="" required="true">
<f:validator validatorId="validation" value="Firstname"/>
<f:attribute name="field" value="Firstname" />
</h:inputText>
<h:message errorStyle="color: red" infoStyle="color: green"layout="table" for="Firstname" />

and my validation class

package za.co.fnb.pyramid.validations;

import java.util.Date;
import javax.faces.validator.*;
import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.component.UIInput;
import javax.faces.context.FacesContext;

import org.apache.log4j.Logger;


public class Validation implements Validator {

Logger LOG = Logger.getLogger(Validation.class);

public Validation() {}


public void validatetitle(FacesContext context,UIComponent toValidate, Object value) {
String title = (String) value;
if (title=="") {
((UIInput)toValidate).setValid(false);
FacesMessage message = new FacesMessage("Select title");
//context.addMessage(toValidate.getClientId(context), message);
throw new ValidatorException(message);

}
}


public void validatename(FacesContext context,UIComponent toValidate, Object value) {
LOG.debug("validatename");
String Firstname = (String) value;
if (Firstname=="") {
((UIInput)toValidate).setValid(false);
FacesMessage message = new FacesMessage("Capture name");
//context.addMessage(toValidate.getClientId(context), message);
throw new ValidatorException(message);

}
}

public void validatesurname(FacesContext context,UIComponent toValidate, Object value) {
LOG.debug("validatesurname");
String surname = (String) value;
if (surname=="") {
((UIInput)toValidate).setValid(false);
FacesMessage message = new FacesMessage("Capture surname");
//context.addMessage(toValidate.getClientId(context), message);
throw new ValidatorException(message);
}
}


public void validateEmail(FacesContext context,UIComponent toValidate, Object value) {
String email = (String) value;
if ( email.indexOf("@") == -1 || email.startsWith("@") || email.endsWith("@") ) {
throw new ValidatorException(new FacesMessage("Enter a valid email address."));
}
}

public void validateIDnumber(FacesContext context, UIComponent toValidate, Object value) {
LOG.debug("validateIDnumber");
String idnumber = (String) value;
if ( idnumber.length() < 13 ) {
((UIInput)toValidate).setValid(false);
FacesMessage message = new FacesMessage("Invalid identity number");
//context.addMessage(toValidate.getClientId(context), message);
throw new ValidatorException(message);
}

}

public void validateDateofBirth(FacesContext context, UIComponent toValidate, Object value) {
LOG.debug("validateDateofBirth");
Date datefield = (Date) value;
if ( datefield==null ) {
((UIInput)toValidate).setValid(false);
FacesMessage message = new FacesMessage("Capture date of birth ");
(context), message);
throw new ValidatorException(message);
}
}







public void validateNumber(FacesContext context, UIComponent component, Object value) {
String s = String.valueOf(value);
if (!s.matches("\\d\\d\\d\\d")){
throw new ValidatorException(new FacesMessage
("Not a four-digit number."));
}
}






@Override
public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException {
String attribute = (String)component.getAttributes().get("value");
LOG.debug("attribute");
if (attribute=="title") {
validatetitle(context, component, value);

} else if (attribute=="Firstname") {
validatename(context, component, value);
LOG.debug("Firstname");

} else if (attribute=="Surname") {
validatesurname(context, component, value);
LOG.debug("Surname");

} else if (attribute=="Email") {
validateEmail(context, component, (String)value);

} else if (attribute=="Id") {
validateIDnumber(context, component, value);

} else if (attribute=="Gender"){
validateGender(context, component,value);

} else if (attribute =="Resident"){
validateResident(context,component,value);

} else if (attribute=="citizen") {
validateCitizenship(context, component, value);

} else if (attribute=="language") {
validateLanguage(context, component, value);

} else if (attribute=="race") {
validateRace(context, component, value);

} else if (attribute=="marital") {
validateMarital(context, component, value);

}else if(attribute=="status"){
validateStatus(context,component,value);

} else if (attribute=="dateofbirth") {
validateDateofBirth(context, component, value);

} else if (attribute=="insolvent") {
validateInsolvent(context, component, value);

} else if (attribute=="rehabilated") {
validateRehabilated(context, component, value);
}

}
}
Yasir Bajwa
Greenhorn

Joined: May 02, 2007
Posts: 23
instead of:
<h:inputText id="Firstname" value="" required="true">
<f:validator validatorId="validation" value="Firstname"/>
<f:attribute name="field" value="Firstname" />
</h:inputText>

try:
<h:inputText id="Firstname" value="******" required="true">
<f:validator validatorId="validation" />
</h:inputText>

replace ****** with the bean / property expression you want to set

You could also just use a function call to do the validation method instead of registering a validator
Kingsley Mullers
Ranch Hand

Joined: Jan 14, 2005
Posts: 48
thanks for your response but how does it find the attribute field since the attribute name needs to be dynamic, so that when i call it from the validation bean it must know which field name to validate.

<h:inputText id="Firstname" value="******" required="true">
<f:validator validatorId="validation" />
</h:inputText>
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JSF validation not validating