1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package javax.faces.validator;
20
21 import javax.faces.application.FacesMessage;
22 import javax.faces.component.UIComponent;
23 import javax.faces.component.UIInput;
24 import javax.faces.context.FacesContext;
25
26 import org.apache.myfaces.buildtools.maven2.plugin.builder.annotation.JSFJspProperty;
27 import org.apache.myfaces.buildtools.maven2.plugin.builder.annotation.JSFProperty;
28 import org.apache.myfaces.buildtools.maven2.plugin.builder.annotation.JSFValidator;
29
30
31
32
33
34
35
36
37
38 @JSFValidator(name = "f:validateRequired",
39 bodyContent = "empty")
40 @JSFJspProperty(name = "binding",
41 returnType = "javax.faces.validator.RequiredValidator",
42 longDesc = "A ValueExpression that evaluates to a RequiredValidator.")
43 public class RequiredValidator implements Validator
44 {
45
46
47 public static final String VALIDATOR_ID = "javax.faces.Required";
48
49
50 public RequiredValidator()
51 {
52 }
53
54
55 public void validate(FacesContext facesContext, UIComponent uiComponent,
56 Object value) throws ValidatorException
57 {
58 if (facesContext == null)
59 {
60 throw new NullPointerException("facesContext");
61 }
62 if (uiComponent == null)
63 {
64 throw new NullPointerException("uiComponent");
65 }
66
67
68 boolean empty = value == null
69 || (value instanceof String && ((String) value).length() == 0);
70
71 if (empty)
72 {
73 if (uiComponent instanceof UIInput)
74 {
75 UIInput uiInput = (UIInput) uiComponent;
76 if (uiInput.getRequiredMessage() != null)
77 {
78 String requiredMessage = uiInput.getRequiredMessage();
79 throw new ValidatorException(new FacesMessage(
80 FacesMessage.SEVERITY_ERROR, requiredMessage,
81 requiredMessage));
82 }
83 }
84 throw new ValidatorException(_MessageUtils.getMessage(facesContext,
85 facesContext.getViewRoot().getLocale(),
86 FacesMessage.SEVERITY_ERROR, UIInput.REQUIRED_MESSAGE_ID,
87 new Object[] { _MessageUtils.getLabel(facesContext,
88 uiComponent) }));
89 }
90 }
91
92 @JSFProperty(faceletsOnly=true)
93 private Boolean isDisabled()
94 {
95 return null;
96 }
97
98 @JSFProperty(faceletsOnly=true)
99 private String getFor()
100 {
101 return null;
102 }
103 }