• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

JSF validation not validating

 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
<h:outputText 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);
}

}
}
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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>
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic