01: // Copyright 2006, 2007 The Apache Software Foundation
02: //
03: // Licensed under the Apache License, Version 2.0 (the "License");
04: // you may not use this file except in compliance with the License.
05: // You may obtain a copy of the License at
06: //
07: // http://www.apache.org/licenses/LICENSE-2.0
08: //
09: // Unless required by applicable law or agreed to in writing, software
10: // distributed under the License is distributed on an "AS IS" BASIS,
11: // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12: // See the License for the specific language governing permissions and
13: // limitations under the License.
14:
15: package org.apache.tapestry.validator;
16:
17: import static org.apache.tapestry.TapestryUtils.quote;
18:
19: import org.apache.tapestry.Field;
20: import org.apache.tapestry.MarkupWriter;
21: import org.apache.tapestry.PageRenderSupport;
22: import org.apache.tapestry.ValidationException;
23: import org.apache.tapestry.Validator;
24: import org.apache.tapestry.ioc.MessageFormatter;
25:
26: /**
27: * A validator that enforces that the value is not null and not the empty string. This validator is
28: * not configurable.
29: */
30: public final class Required implements Validator<Void, Object> {
31: public String getMessageKey() {
32: return "required";
33: }
34:
35: public void validate(Field field, Void constraintValue,
36: MessageFormatter formatter, Object value)
37: throws ValidationException {
38: if (value == null || value.toString().equals(""))
39: throw new ValidationException(
40: buildMessage(formatter, field));
41: }
42:
43: private String buildMessage(MessageFormatter formatter, Field field) {
44: return formatter.format(field.getLabel());
45: }
46:
47: public Class<Void> getConstraintType() {
48: return null;
49: }
50:
51: public boolean invokeIfBlank() {
52: return true;
53: }
54:
55: public Class<Object> getValueType() {
56: return Object.class;
57: }
58:
59: public void render(Field field, Void constraintValue,
60: MessageFormatter formatter, MarkupWriter writer,
61: PageRenderSupport pageRenderSupport) {
62: pageRenderSupport.addScript(
63: "Tapestry.Field.required('%s', %s);", field
64: .getClientId(), quote(buildMessage(formatter,
65: field)));
66:
67: }
68: }
|