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 @JSFValidator(name = "f:validateRequired",
37 bodyContent = "empty")
38 @JSFJspProperty(name = "binding",
39 returnType = "javax.faces.validator.RequiredValidator",
40 longDesc = "A ValueExpression that evaluates to a RequiredValidator.")
41 public class RequiredValidator implements Validator
42 {
43
44
45 public static final String VALIDATOR_ID = "javax.faces.Required";
46
47
48 public RequiredValidator()
49 {
50 }
51
52
53 public void validate(FacesContext facesContext, UIComponent uiComponent,
54 Object value) throws ValidatorException
55 {
56 if (facesContext == null)
57 {
58 throw new NullPointerException("facesContext");
59 }
60 if (uiComponent == null)
61 {
62 throw new NullPointerException("uiComponent");
63 }
64
65
66 boolean empty = value == null
67 || (value instanceof String && ((String) value).length() == 0);
68
69 if (empty)
70 {
71 if (uiComponent instanceof UIInput)
72 {
73 UIInput uiInput = (UIInput) uiComponent;
74 if (uiInput.getRequiredMessage() != null)
75 {
76 String requiredMessage = uiInput.getRequiredMessage();
77 throw new ValidatorException(new FacesMessage(
78 FacesMessage.SEVERITY_ERROR, requiredMessage,
79 requiredMessage));
80 }
81 }
82 throw new ValidatorException(_MessageUtils.getMessage(facesContext,
83 facesContext.getViewRoot().getLocale(),
84 FacesMessage.SEVERITY_ERROR, UIInput.REQUIRED_MESSAGE_ID,
85 new Object[] { _MessageUtils.getLabel(facesContext,
86 uiComponent) }));
87 }
88 }
89
90 @JSFProperty(faceletsOnly=true)
91 @SuppressWarnings("unused")
92 private Boolean isDisabled()
93 {
94 return null;
95 }
96
97 @JSFProperty(faceletsOnly=true)
98 @SuppressWarnings("unused")
99 private String getFor()
100 {
101 return null;
102 }
103 }