01: // Copyright 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: * Validates that a string value has a minimum length.
28: */
29: public final class MinLength implements Validator<Integer, String> {
30: public String getMessageKey() {
31: return "minimum-string-length";
32: }
33:
34: public void validate(Field field, Integer constraintValue,
35: MessageFormatter formatter, String value)
36: throws ValidationException {
37: if (value.length() < constraintValue)
38: throw new ValidationException(buildMessage(formatter,
39: field, constraintValue));
40: }
41:
42: private String buildMessage(MessageFormatter formatter,
43: Field field, Integer constraintValue) {
44: return formatter.format(constraintValue, field.getLabel());
45: }
46:
47: public Class<Integer> getConstraintType() {
48: return Integer.class;
49: }
50:
51: public boolean invokeIfBlank() {
52: return false;
53: }
54:
55: public Class<String> getValueType() {
56: return String.class;
57: }
58:
59: public void render(Field field, Integer constraintValue,
60: MessageFormatter formatter, MarkupWriter writer,
61: PageRenderSupport pageRenderSupport) {
62: pageRenderSupport.addScript(
63: "Tapestry.Field.minlength('%s', %d, %s);", field
64: .getClientId(), constraintValue,
65: quote(buildMessage(formatter, field, constraintValue)));
66: }
67: }
|