Created
December 27, 2011 19:26
-
-
Save biemond/1524868 to your computer and use it in GitHub Desktop.
JSF bean validation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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