• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Custom input validator

 
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've got this code in my project to validate fields, i've made this my backend bean and added it to my faces.config.xml.when i call this bean to validate the fields, nothing happens. i call my bean like this, validation being the name of the class and validate surname is the method name in my input attribute, can anybody tell me what i'm doing wrong. i call individual methods for each and every field, to validate but no luck

<h:outputText value="Surname" />
<h:inputText id="Surname" value="" required="true" validator="#{validation.validatesurname}" />
<h:message errorStyle="color: red"
infoStyle="color: green" layout="table" for="Surname" />

Back end bean


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 javax.faces.convert.ConverterException;

import com.sun.org.apache.xerces.internal.impl.xpath.regex.ParseException;

public class Validation implements Validator {


public Date validateDate(FacesContext context, UIComponent component, String value) throws ConverterException, java.text.ParseException {

String pattern = "yyyy/MM/dd";
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
Date nDate;

try {

nDate = sdf.parse(value);
} catch (ParseException ex) {
FacesMessage message = new FacesMessage();
message.setDetail("Date required (yyyy/MM/dd)");
message.setSummary("Date required (yyyy/MM/dd)");
message.setSeverity(FacesMessage.SEVERITY_ERROR);
throw new ConverterException(message);
}
if(nDate.getTime() > new Date().getTime()){
FacesMessage message = new FacesMessage();
message.setDetail("Invalid birth day to ID number");
message.setSummary("Invalid birth day to ID number");
message.setSeverity(FacesMessage.SEVERITY_ERROR);
throw new ConverterException(message);
}

return nDate;

}
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 validatesurname(FacesContext context,UIComponent toValidate, Object value) {
String surname = (String) value;
LOG.debug("validatesurnname");
if (surname=="") {
((UIInput)toValidate).setValid(false);
FacesMessage message = new FacesMessage("Capture surname");
context.addMessage(toValidate.getClientId(context), message);
LOG.debug("validatesurname");
}
}
public void validatePassport(FacesContext context, UIComponent toValidate, Object value) {
String passport = (String) value;
if ( passport=="" ) {
((UIInput)toValidate).setValid(false);
FacesMessage message = new FacesMessage("Invalid passport");
context.addMessage(toValidate.getClientId(context), message);
}

}
public void validateAddress(FacesContext context, UIComponent toValidate, Object value) {
String address = (String) value;
if ( address=="" ) {
((UIInput)toValidate).setValid(false);
FacesMessage message = new FacesMessage("Capture address");
context.addMessage(toValidate.getClientId(context), message);
}

}
public void CellNumberValidator(FacesContext context, UIComponent component, Object value) {
String strValue = (String)value;
if (strValue.length() != 10){
throwException("Number of cell phone digits must be 10");
}
}



}


 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not sure, but it doesn't look like the Validator interface is actually implemented. How is it compiling?

In your case with method calls I don't think you need to actually implement the Validator interface, but the validation methods should have methods with the same signature as the validate method in the Validate interface.

The validateDate method doesn't look quite right, I think java.text.ParseException is a checked exception, so it probably won't work as a validator method. I guess you could wrap any checked exception into a validator exception?
 
reply
    Bookmark Topic Watch Topic
  • New Topic