Friday, February 24, 2012

JSF Validation

1. Customize error message.




You can only have one validatorMessage. So if you have multiple validators, please use custom validators.

2. For default validator, if you want customrize message, you can do this too.





put this into resources bundle.
javax.faces.validator.LongRangeValidator.MAXIMUM={1}: Value is greater than allowable maximum of ''{0}''

3. How to specify field name in validator.







private void checkDate(Date date, UIComponent uiComponent, Locale locale) {
if(isDateInRange(date) == false) {
ResourceBundle rb = ResourceBundle.getBundle("messages", locale);
String messageText = getFieldLabel(uiComponent) +" " + rb.getString(getErrorKey());

throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_ERROR,
messageText, messageText));
}
}

protected String getFieldLabel(UIComponent uiComponent) {
String fieldLabel = (String) uiComponent.getAttributes().get("fieldLabel");

if(fieldLabel == null) {
fieldLabel = "Date" ;
}

return fieldLabel;
}

Check this link for details.

No comments:

Post a Comment