-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMethodValidationExamples.java
More file actions
42 lines (33 loc) · 1.39 KB
/
MethodValidationExamples.java
File metadata and controls
42 lines (33 loc) · 1.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package org.javaee7.chapter06;
import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.component.UIInput;
import javax.faces.context.FacesContext;
import javax.validation.Validation;
import javax.validation.executable.ExecutableValidator;
/**
*
* @author Juneau
*/
public class MethodValidationExamples {
public MethodValidationExamples(){}
public ExecutableValidator getExecutableValidator(){
ExecutableValidator executableValidator = Validation
.buildDefaultValidatorFactory().getValidator().forExecutables();
return executableValidator;
}
public void validateEmployeeId(FacesContext context,
UIComponent component,
Object value){
String idValue = (String) value;
boolean result = idValue.matches("[0-9]{7}[JJ]");
if (!result) {
FacesMessage message = new FacesMessage();
message.setSeverity(FacesMessage.SEVERITY_ERROR);
message.setSummary(component.getClientId().substring(component.getClientId().indexOf(":") + 1) + " issue, please use the format XXXXXXXJJ");
context.addMessage(component.getClientId(), message);
// Set the component's valid attribute to false
((UIInput) component).setValid(false);
}
}
}