iScreen validations provides server side validations. It provides elaborate documentation on how to implement your validations. There are two kinds of projects which need iScreen.
1) Project is in initial phases
2) Project is in advanced stages and we suddenly discover that there are no server side validations.
The later use case is challenging and tricky. This is my first blog entry in a series where I ll discuss how we have effectively implemented iScreen in a large project which has around 1600 separate screens.
Handling String Arrays
iScreen provides 'org.iscreen.StringValidator'. It provides length validation. However it does not handle values in an array. In our code we are passing requestMap as object to be validated. The requesr map contains request paramters as keys and values as haspmap value. Servlet creates an array of values when there are multiple fields with same name in the html form. The following code will not validate all the values in the "orderId" field.
<use-validator ref="org.iscreen.StringValidator" name="orderId">
<mapping from="#root.orderId" to="value" />
<label>Order Id</label>
<constraint property="minLength">10</constraint>
<constraint property="maxLength">10</constraint>
</use-validator>
First solution is modify the OGNL expression. The ognl expression will check the data type if it is an array then pass the first value in the array. The updated code
<use-validator ref="org.iscreen.StringValidator" name="orderId">
<mapping from="#root.orderId instanceof java.lang.String[] ? #root.orderId[0] : root.orderId" to="value" />
<label>Order Id</label>
<constraint property="minLength">10</constraint>
<constraint property="maxLength">10</constraint>
</use-validator>
The above code will only validate the first value in the array. This not sufficient in most cases
Solution is to extend the org.iscreen.StringValidator to handle array of values.
Following is the modified StringValidator class.
1) Project is in initial phases
2) Project is in advanced stages and we suddenly discover that there are no server side validations.
The later use case is challenging and tricky. This is my first blog entry in a series where I ll discuss how we have effectively implemented iScreen in a large project which has around 1600 separate screens.
Handling String Arrays
iScreen provides 'org.iscreen.StringValidator'. It provides length validation. However it does not handle values in an array. In our code we are passing requestMap as object to be validated. The requesr map contains request paramters as keys and values as haspmap value. Servlet creates an array of values when there are multiple fields with same name in the html form. The following code will not validate all the values in the "orderId" field.
<use-validator ref="org.iscreen.StringValidator" name="orderId">
<mapping from="#root.orderId" to="value" />
<label>Order Id</label>
<constraint property="minLength">10</constraint>
<constraint property="maxLength">10</constraint>
</use-validator>
First solution is modify the OGNL expression. The ognl expression will check the data type if it is an array then pass the first value in the array. The updated code
<use-validator ref="org.iscreen.StringValidator" name="orderId">
<mapping from="#root.orderId instanceof java.lang.String[] ? #root.orderId[0] : root.orderId" to="value" />
<label>Order Id</label>
<constraint property="minLength">10</constraint>
<constraint property="maxLength">10</constraint>
</use-validator>
The above code will only validate the first value in the array. This not sufficient in most cases
Solution is to extend the org.iscreen.StringValidator to handle array of values.
Following is the modified StringValidator class.
CustomStringValidator.java |
1 /*
2 * Copyright 2006 Dan Shellman
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.iscreen.validators;
17
18 import org.iscreen.SimpleBean;
19 import org.iscreen.ValidatorContext;
20
21 /**
22 * The CustomStringValidator checks the length of the given value in an array.
23 *
24 * @author Shrikant Sarda
25 */
26 public class CustomStringValidator extends StringValidator {
27
28 /**
29 * Default constructor.
30 */
31 public CustomStringValidator() {
32 } //end StringValidator()
33
34 public void validate(ValidatorContext context, Object beanToValidate) {
35 String value;
36 int valueLength;
37 String[] arrValue = null;
38
39 if (beanToValidate != null && ((SimpleBean) beanToValidate).getValue() != null) {
40 if (((SimpleBean) beanToValidate).getValue() instanceof String[]) {
41 arrValue = (String[]) ((SimpleBean) beanToValidate).getValue();
42 }
43 }
44
45 if (arrValue != null) {
46 //System.out.println("in if");
47 //String[] customBeanToValidate = (String[])beanToValidate;
48 for (int i = 0; i < class="line-number">49 value = arrValue[i];
50 if (value == null) {
51 if (minLength != null && minLength.intValue() > 0) {
52 context.reportFailure(nullFailure);
53 }
54
55 return;
56 }
57
58 valueLength = value.length();
59 if (minLength != null &&
60 valueLength < class="line-number">61 context.reportFailure(minLengthFailure, new Integer(valueLength));
62 } else if (maxLength != null && maxLength.intValue() == 1 &&
63 valueLength == 1) {
64 //System.out.println("valueLength,78:" + valueLength + ",value:" + value);
65 } else if (maxLength != null &&
66 valueLength > maxLength.intValue()) {
67 context.reportFailure(maxLengthFailure, new Integer(valueLength));
68 }
69
70 } //end for
71 }// end if
72 else {
73 //System.out.println("in else");
74 value = getStringValue(beanToValidate);
75 if (value == null) {
76 if (minLength != null && minLength.intValue() > 0) {
77 context.reportFailure(nullFailure);
78 }
79
80 return;
81 }
82
83 valueLength = value.length();
84 if (minLength != null &&
85 valueLength < class="line-number">86 context.reportFailure(minLengthFailure, new Integer(valueLength));
87 } else if (maxLength != null && maxLength.intValue() == 1 &&
88 valueLength == 1) {
89 //System.out.println("valueLength,113:" + valueLength + ",value:" + value);
90 } else if (maxLength != null &&
91 valueLength > maxLength.intValue()) {
92 //System.out.println("valueLength,119:" + valueLength + ",value:" + value);
93 context.reportFailure(maxLengthFailure, new Integer(valueLength));
94 }
95 }//end else
96 } //end validate()
97 } //end CustomStringValidator
98
99
No comments:
Post a Comment