KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > myfaces > examples > example1 > ValidationController


1 /*
2  * Copyright 2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.myfaces.examples.example1;
17
18 import javax.faces.component.UIInput;
19 import javax.faces.context.FacesContext;
20 import javax.faces.validator.LengthValidator;
21 import javax.faces.validator.LongRangeValidator;
22 import javax.faces.validator.Validator;
23
24 /**
25  * DOCUMENT ME!
26  * @author Manfred Geiler
27  * @version $Revision: 1.1 $ $Date: 2005/03/24 16:47:10 $
28  */

29 public class ValidationController
30 {
31     public String JavaDoc enableValidation()
32     {
33         FacesContext facesContext = FacesContext.getCurrentInstance();
34
35         UIInput number1 = (UIInput)facesContext.getViewRoot().findComponent("form1:number1");
36         Validator[] validators = number1.getValidators();
37         if (validators == null || validators.length == 0)
38         {
39             number1.addValidator(new LongRangeValidator(10, 1));
40         }
41
42         UIInput number2 = (UIInput)facesContext.getViewRoot().findComponent("form1:number2");
43         validators = number2.getValidators();
44         if (validators == null || validators.length == 0)
45         {
46             number2.addValidator(new LongRangeValidator(50, 20));
47         }
48
49         UIInput text = (UIInput)facesContext.getViewRoot().findComponent("form2:text");
50         validators = text.getValidators();
51         if (validators == null || validators.length == 0)
52         {
53             text.addValidator(new LengthValidator(7, 3));
54         }
55
56         return "ok";
57     }
58
59     public String JavaDoc disableValidation()
60     {
61         FacesContext facesContext = FacesContext.getCurrentInstance();
62
63         UIInput number1 = (UIInput)facesContext.getViewRoot().findComponent("form1:number1");
64         Validator[] validators = number1.getValidators();
65         if (validators != null)
66         {
67             for (int i = 0; i < validators.length; i++)
68             {
69                 Validator validator = validators[i];
70                 number1.removeValidator(validator);
71             }
72         }
73
74         UIInput number2 = (UIInput)facesContext.getViewRoot().findComponent("form1:number2");
75         validators = number2.getValidators();
76         if (validators != null)
77         {
78             for (int i = 0; i < validators.length; i++)
79             {
80                 Validator validator = validators[i];
81                 number2.removeValidator(validator);
82             }
83         }
84
85         UIInput text = (UIInput)facesContext.getViewRoot().findComponent("form2:text");
86         validators = text.getValidators();
87         if (validators != null)
88         {
89             for (int i = 0; i < validators.length; i++)
90             {
91                 Validator validator = validators[i];
92                 text.removeValidator(validator);
93             }
94         }
95
96         return "ok";
97     }
98
99
100
101     public String JavaDoc getNumber1ValidationLabel()
102     {
103         FacesContext facesContext = FacesContext.getCurrentInstance();
104         UIInput number1 = (UIInput)facesContext.getViewRoot().findComponent("form1:number1");
105         Validator[] validators = number1.getValidators();
106         if (validators != null && validators.length > 0)
107         {
108             long min = ((LongRangeValidator)validators[0]).getMinimum();
109             long max = ((LongRangeValidator)validators[0]).getMaximum();
110             return " (" + min + "-" + max + ")";
111         }
112         else
113         {
114             return "";
115         }
116     }
117
118     public String JavaDoc getNumber2ValidationLabel()
119     {
120         FacesContext facesContext = FacesContext.getCurrentInstance();
121         UIInput number1 = (UIInput)facesContext.getViewRoot().findComponent("form1:number2");
122         Validator[] validators = number1.getValidators();
123         if (validators != null && validators.length > 0)
124         {
125             long min = ((LongRangeValidator)validators[0]).getMinimum();
126             long max = ((LongRangeValidator)validators[0]).getMaximum();
127             return " (" + min + "-" + max + ")";
128         }
129         else
130         {
131             return "";
132         }
133     }
134
135     public String JavaDoc getTextValidationLabel()
136     {
137         FacesContext facesContext = FacesContext.getCurrentInstance();
138         UIInput number1 = (UIInput)facesContext.getViewRoot().findComponent("form2:text");
139         Validator[] validators = number1.getValidators();
140         if (validators != null && validators.length > 0)
141         {
142             long min = ((LengthValidator)validators[0]).getMinimum();
143             long max = ((LengthValidator)validators[0]).getMaximum();
144             return " (" + min + "-" + max + " chars)";
145         }
146         else
147         {
148             return "";
149         }
150     }
151
152 }
153
Popular Tags