ValidationHelper.java
7.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* javax.servlet.ServletException
* javax.servlet.http.HttpServletRequest
* javax.servlet.jsp.JspWriter
* org.apache.commons.lang3.StringEscapeUtils
* org.apache.sling.api.SlingHttpServletRequest
* org.apache.sling.api.SlingHttpServletResponse
* org.apache.sling.api.resource.Resource
* org.apache.sling.api.resource.ResourceUtil
* org.apache.sling.api.resource.ValueMap
*/
package com.day.cq.wcm.foundation.forms;
import com.day.cq.wcm.foundation.forms.FormsHelper;
import com.day.cq.wcm.foundation.forms.ValidationInfo;
import com.day.cq.wcm.foundation.forms.impl.JspSlingHttpServletResponseWrapper;
import com.day.cq.wcm.foundation.forms.impl.ResourceWrapper;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspWriter;
import org.apache.commons.lang3.StringEscapeUtils;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceUtil;
import org.apache.sling.api.resource.ValueMap;
@Deprecated
public class ValidationHelper {
private ValidationHelper() {
}
@Deprecated
public static ValidationInfo getValidationInfo(SlingHttpServletRequest request) {
return ValidationInfo.createValidationInfo(request);
}
@Deprecated
public static boolean hasValidationInfo(HttpServletRequest request) {
return ValidationInfo.getValidationInfo(request) != null;
}
@Deprecated
public static String getRequiredMessage(Resource resource) {
ValueMap properties = ResourceUtil.getValueMap((Resource)resource);
String msg = (String)properties.get("requiredMessage", (Object)"");
if (msg.length() == 0) {
msg = "Field is required.";
}
return msg;
}
@Deprecated
public static String getConstraintMessage(Resource resource) {
ValueMap properties = ResourceUtil.getValueMap((Resource)resource);
String msg = (String)properties.get("constraintMessage", (Object)"");
if (msg.length() == 0) {
msg = "Field is not valid.";
}
return msg;
}
@Deprecated
public static boolean checkRequired(SlingHttpServletRequest request, Resource resource) {
boolean isEmpty;
String name = FormsHelper.getParameterName(resource);
String value = request.getParameter(name);
boolean bl = isEmpty = value == null || value.trim().length() == 0;
if (FormsHelper.isRequired(resource) && isEmpty) {
ValidationInfo.createValidationInfo(request).addErrorMessage(name, ValidationHelper.getRequiredMessage(resource));
return false;
}
return true;
}
@Deprecated
public static void checkConstraint(SlingHttpServletRequest request, SlingHttpServletResponse response, Resource resource) throws IOException, ServletException {
String constraint;
boolean isEmpty;
ValueMap properties = ResourceUtil.getValueMap((Resource)resource);
String name = FormsHelper.getParameterName(resource);
String value = request.getParameter(name);
boolean bl = isEmpty = value == null || value.trim().length() == 0;
if (!isEmpty && (constraint = (String)properties.get("constraintType", (Object)"")).length() > 0) {
String rt = constraint;
if (constraint.indexOf(47) == -1) {
rt = "foundation/components/form/constraints/" + rt;
}
ResourceWrapper includeResource = new ResourceWrapper(resource, rt, "foundation/components/form/defaults/constraint");
FormsHelper.includeResource(request, response, (Resource)includeResource, "servervalidation");
}
}
@Deprecated
public static void writeRequiredCheck(SlingHttpServletRequest request, Resource resource, JspWriter out) throws IOException {
String formId = FormsHelper.getFormId(request);
String name = FormsHelper.getParameterName(resource);
if (FormsHelper.isRequired(resource)) {
String qualifier = ValidationHelper.getFormElementQualifier(request, resource);
out.print("if (cq5forms_isEmpty(");
out.print(qualifier);
out.print(")) {cq5forms_showMsg('");
out.print(StringEscapeUtils.escapeEcmaScript((String)formId));
out.print("','");
out.print(StringEscapeUtils.escapeEcmaScript((String)name));
out.print("','");
out.print(StringEscapeUtils.escapeEcmaScript((String)ValidationHelper.getRequiredMessage(resource)));
out.println("'); return false; }");
}
}
@Deprecated
public static String getFormElementQualifier(SlingHttpServletRequest request, Resource resource) {
String formId = FormsHelper.getFormId(request);
String name = FormsHelper.getParameterName(resource);
return "document.forms[\"" + StringEscapeUtils.escapeEcmaScript((String)formId) + "\"]" + ".elements[\"" + StringEscapeUtils.escapeEcmaScript((String)name) + "\"]";
}
@Deprecated
public static void writeConstraintCheck(SlingHttpServletRequest request, SlingHttpServletResponse response, Resource resource, JspWriter out) throws IOException, ServletException {
ValueMap properties = ResourceUtil.getValueMap((Resource)resource);
String constraint = (String)properties.get("constraintType", (Object)"");
if (constraint.length() > 0) {
String qualifier = ValidationHelper.getFormElementQualifier(request, resource);
out.print("if (!cq5forms_isEmpty(");
out.print(qualifier);
out.print(")){");
String rt = constraint;
if (constraint.indexOf(47) == -1) {
rt = "foundation/components/form/constraints/" + rt;
}
ResourceWrapper includeResource = new ResourceWrapper(resource, rt, "foundation/components/form/defaults/constraint");
FormsHelper.includeResource(request, (SlingHttpServletResponse)new JspSlingHttpServletResponseWrapper(response, out), (Resource)includeResource, "clientvalidation");
out.print("}");
}
}
@Deprecated
public static void writeRegexpText(SlingHttpServletRequest request, Resource resource, String regexp, JspWriter out) throws IOException {
String id = ValidationHelper.getFormElementQualifier(request, resource);
String name = FormsHelper.getParameterName(resource);
out.println("{ var result=false;");
out.print("var pattern = ");
out.print(regexp);
out.print("; var t = pattern.exec(");
out.print(id);
out.print(".value);");
out.println("if (t) {");
out.println("var len = ");
out.print(id);
out.print(".value.length;");
out.println("var pattlen = t[0].length;");
out.println("result = (pattlen == len); ");
out.println("}");
out.println("if ( !result ) {");
out.print("cq5forms_showMsg('");
out.print(StringEscapeUtils.escapeEcmaScript((String)FormsHelper.getFormId(request)));
out.print("','");
out.print(StringEscapeUtils.escapeEcmaScript((String)name));
out.print("','");
out.print(StringEscapeUtils.escapeEcmaScript((String)ValidationHelper.getConstraintMessage(resource)));
out.print("');");
out.println("return false; } }");
}
}