KickJava   Java API By Example, From Geeks To Geeks.

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


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.hivemind.util.PropertyUtils;
18 import org.apache.tapestry.IMarkupWriter;
19 import org.apache.tapestry.IRequestCycle;
20 import org.apache.tapestry.form.FormComponentContributorContext;
21 import org.apache.tapestry.form.IFormComponent;
22 import org.apache.tapestry.form.ValidationMessages;
23 import org.apache.tapestry.valid.ValidationConstraint;
24 import org.apache.tapestry.valid.ValidationStrings;
25 import org.apache.tapestry.valid.ValidatorException;
26
27 /**
28  * {@link org.apache.tapestry.form.validator.Validator} that ensures a value was supplied.
29  *
30  * @author Howard Lewis Ship
31  * @since 4.0
32  */

33 public class Required extends BaseValidator
34 {
35     public Required()
36     {
37     }
38
39     public Required(String JavaDoc initializer)
40     {
41         super(initializer);
42     }
43
44     public boolean getAcceptsNull()
45     {
46         return true;
47     }
48
49     public void validate(IFormComponent field, ValidationMessages messages, Object JavaDoc object)
50             throws ValidatorException
51     {
52         if (object == null)
53         {
54             String JavaDoc message = buildMessage(messages, field);
55             throw new ValidatorException(message, ValidationConstraint.REQUIRED);
56         }
57     }
58
59     private String JavaDoc buildMessage(ValidationMessages messages, IFormComponent field)
60     {
61         return messages.formatValidationMessage(
62                 getMessage(),
63                 ValidationStrings.REQUIRED_TEXT_FIELD,
64                 new Object JavaDoc[]
65                 { field.getDisplayName() });
66     }
67
68     public void renderContribution(IMarkupWriter writer, IRequestCycle cycle,
69             FormComponentContributorContext context, IFormComponent field)
70     {
71         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc("function(event) { require(event, ");
72         buffer.append(context.getFieldDOM());
73         buffer.append(", '");
74         buffer.append(buildMessage(context, field));
75         buffer.append("'); }");
76
77         context.addSubmitListener(buffer.toString());
78     }
79
80 }
81
Popular Tags