KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > form > validator > MinLength


1 // Copyright 2005 The Apache Software Foundation
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// 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.form.validator;
16
17 import org.apache.tapestry.IMarkupWriter;
18 import org.apache.tapestry.IRequestCycle;
19 import org.apache.tapestry.form.FormComponentContributorContext;
20 import org.apache.tapestry.form.IFormComponent;
21 import org.apache.tapestry.form.ValidationMessages;
22 import org.apache.tapestry.valid.ValidationConstraint;
23 import org.apache.tapestry.valid.ValidationStrings;
24 import org.apache.tapestry.valid.ValidatorException;
25
26 /**
27  * Validates that the value, a string, is of a minimum length.
28  *
29  * @author Howard Lewis Ship
30  * @since 4.0
31  */

32 public class MinLength extends BaseValidator
33 {
34     private int _minLength;
35
36     public MinLength()
37     {
38     }
39
40     public MinLength(String JavaDoc initializer)
41     {
42         super(initializer);
43     }
44
45     public void setMinLength(int minLength)
46     {
47         _minLength = minLength;
48     }
49
50     public int getMinLength()
51     {
52         return _minLength;
53     }
54
55     public void validate(IFormComponent field, ValidationMessages messages, Object JavaDoc object)
56             throws ValidatorException
57     {
58         String JavaDoc string = (String JavaDoc) object;
59
60         if (string.length() < _minLength)
61             throw new ValidatorException(buildMessage(messages, field),
62                     ValidationConstraint.MINIMUM_WIDTH);
63     }
64
65     protected String JavaDoc buildMessage(ValidationMessages messages, IFormComponent field)
66     {
67         return messages.formatValidationMessage(
68                 getMessage(),
69                 ValidationStrings.VALUE_TOO_SHORT,
70                 new Object JavaDoc[]
71                 { new Integer JavaDoc(_minLength), field.getDisplayName() });
72     }
73
74     public void renderContribution(IMarkupWriter writer, IRequestCycle cycle,
75             FormComponentContributorContext context, IFormComponent field)
76     {
77         context.includeClasspathScript("/org/apache/tapestry/form/validator/StringValidator.js");
78
79         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc("function(event) { validate_min_length(event, ");
80         buffer.append(context.getFieldDOM());
81         buffer.append(", ");
82         buffer.append(_minLength);
83         buffer.append(", '");
84         buffer.append(buildMessage(context, field));
85         buffer.append("'); }");
86
87         context.addSubmitListener(buffer.toString());
88     }
89 }
Popular Tags