This week's book giveaway is in the Agile and other Processes forum. We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line! See this thread for details.
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" />
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.")); } }
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
posted
0
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.