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 throw new NullPointerException("facesContext");
60 if (uiComponent == null)
61 throw new NullPointerException("uiComponent");
62
63
64 boolean empty = value == null
65 || (value instanceof String && ((String) value).length() == 0);
66
67 if (empty)
68 {
69 if (uiComponent instanceof UIInput)
70 {
71 UIInput uiInput = (UIInput) uiComponent;
72 if (uiInput.getRequiredMessage() != null)
73 {
74 String requiredMessage = uiInput.getRequiredMessage();
75 throw new ValidatorException(new FacesMessage(
76 FacesMessage.SEVERITY_ERROR, requiredMessage,
77 requiredMessage));
78 }
79 }
80 throw new ValidatorException(_MessageUtils.getMessage(facesContext,
81 facesContext.getViewRoot().getLocale(),
82 FacesMessage.SEVERITY_ERROR, UIInput.REQUIRED_MESSAGE_ID,
83 new Object[] { _MessageUtils.getLabel(facesContext,
84 uiComponent) }));
85 }
86 }
87
88 @JSFProperty(faceletsOnly=true)
89 private Boolean isDisabled()
90 {
91 return null;
92 }
93
94 @JSFProperty(faceletsOnly=true)
95 private String getFor()
96 {
97 return null;
98 }
99 }