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 package nl.openedge.baritus.validation.impl;
32
33 import nl.openedge.baritus.FormBeanContext;
34 import nl.openedge.baritus.validation.AbstractFieldValidator;
35 import nl.openedge.baritus.validation.ValidationActivationRule;
36
37 import org.infohazard.maverick.flow.ControllerContext;
38
39 /***
40 * Checks for a non-EMPTY input.
41 * Use this for fields that should have a not null (empty string) input.
42 * Note that as this validator is a field validator, and thus is registered
43 * for a single field, it is only fired if a field (e.g. a request parameter)
44 * is actually provided. In other words: if an instance of a required field
45 * validator was registered for field name 'myprop', but 'myprop' is not part
46 * of the request parameters, this validator never fires. Hence, if you want
47 * to be REALLY sure that a property of the form is not null, use a form validator
48 * (PropertyNotNullValidator). RequiredFieldValidator works fine for most cases
49 * where you have a HTML form with a field that should have a non empty value, but
50 * that - if a user fools around - does not seriousely break anything when a value
51 * is not provided (e.g. you probably have not null constraint in you database as well).
52 *
53 * @author Eelco Hillenius
54 */
55 public class RequiredFieldValidator extends AbstractFieldValidator
56 {
57
58 private String errorMessageKey = "input.field.required";
59
60 /***
61 * Construct using 'input.field.required' as the message prefix.
62 */
63 public RequiredFieldValidator()
64 {
65
66 }
67
68 /***
69 * Construct using the provided activation rule and 'input.field.required' as the message prefix.
70 * @param rule
71 */
72 public RequiredFieldValidator(ValidationActivationRule rule)
73 {
74 setValidationRule(rule);
75 }
76
77 /***
78 * Construct using errorMessageKey and the activation rule
79 * @param errorMessageKey
80 * @param rule
81 */
82 public RequiredFieldValidator(
83 String errorMessageKey,
84 ValidationActivationRule rule)
85 {
86 setErrorMessageKey(errorMessageKey);
87 setValidationRule(rule);
88 }
89
90 /***
91 * Construct with errorMessageKey for error message keys.
92 * @param errorMessageKey errorMessageKey
93 */
94 public RequiredFieldValidator(String errorMessageKey)
95 {
96 setErrorMessageKey(errorMessageKey);
97 }
98
99 /***
100 * Checks whether the value is not null, and - if it is an instance of String - whether
101 * the trimmed value is not an empty string.
102 * Note that as this validator is a field validator, and thus is registered
103 * for a single field, it is only fired if a field (e.g. a request parameter)
104 * is actually provided. In other words: if an instance of a required field
105 * validator was registered for field name 'myprop', but 'myprop' is not part
106 * of the request parameters, this validator never fires. Hence, if you want
107 * to be REALLY sure that a property of the form is not null, use a form validator
108 * (PropertyNotNullValidator). RequiredFieldValidator works fine for most cases
109 * where you have a HTML form with a field that should have a non empty value, but
110 * that - if a user fools around - does not seriousely break anything when a value
111 * is not provided (e.g. you probably have not null constraint in you database as well).
112 *
113 * @return boolean true if not null or empty, false otherwise
114 *
115 * @see nl.openedge.baritus.validation.FieldValidator#isValid(org.infohazard.maverick.flow.ControllerContext, nl.openedge.baritus.FormBeanContext, java.lang.String, java.lang.Object)
116 */
117 public boolean isValid(
118 ControllerContext cctx,
119 FormBeanContext formBeanContext,
120 String fieldName,
121 Object value)
122 {
123 boolean isValid = true;
124 if(value == null)
125 {
126 isValid = false;
127 }
128 else
129 {
130 if(value instanceof String)
131 {
132 if("".equals(((String)value).trim()))
133 {
134 isValid = false;
135 }
136 }
137 }
138
139 if(!isValid)
140 {
141 setErrorMessage(formBeanContext, fieldName, getErrorMessageKey(),
142 new Object[]{getFieldName(formBeanContext, fieldName)});
143 }
144
145 return isValid;
146 }
147
148 /***
149 * Get key of error message.
150 * @return String key of error message
151 */
152 public String getErrorMessageKey()
153 {
154 return errorMessageKey;
155 }
156
157 /***
158 * Set key of error message.
159 * @param string key of error message
160 */
161 public void setErrorMessageKey(String string)
162 {
163 errorMessageKey = string;
164 }
165
166 }