Skip to content

Instantly share code, notes, and snippets.

@biemond
Created December 27, 2011 19:26
Show Gist options
  • Save biemond/1524868 to your computer and use it in GitHub Desktop.
Save biemond/1524868 to your computer and use it in GitHub Desktop.
JSF bean validation
private Validator validator;
public void addDepartment(ActionEvent event) {
if (validator == null) {
validator = Validation.buildDefaultValidatorFactory().getValidator();
}
Department dept = new Department();
dept.setDepartmentName("1234567890123456789012345678901234567890");
Set<ConstraintViolation<Department>> violations = validator.validate(dept);
for (ConstraintViolation<Department> violation : violations) {
System.out.println("error size: "+violations.size());
String propertyPath = violation.getPropertyPath().toString();
String message = violation.getMessage();
FacesMessage message2 =
new FacesMessage(FacesMessage.SEVERITY_ERROR,
"Invalid value for: '" + propertyPath + "': " + message,
"Validation Error ");
FacesContext context = FacesContext.getCurrentInstance();
context.addMessage(event.getComponent().getClientId(), message2);
}
if ( violations.size() > 0 ) {
return;
}
getSessionFacade().persistDepartment(dept);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment