cmsformhandler.java
来自「找了很久才找到到源代码」· Java 代码 · 共 810 行 · 第 1/3 页
JAVA
810 行
// input field validation failed
result = true;
if (getFormConfiguration().hasCaptchaField() && getFormConfiguration().captchaFieldIsOnCheckPage()) {
// if there is a captcha field and a check page configured, we do have to remove the already
// initialized captcha field from the form again. the captcha field gets initialized together with
// the form, in this moment it is not clear yet whether we have validation errors or and need to
// to go back to the input form...
getFormConfiguration().removeCaptchaField();
}
} else if (ACTION_CONFIRMED.equalsIgnoreCase(getParameter(PARAM_FORMACTION))
&& getFormConfiguration().captchaFieldIsOnCheckPage()
&& !validate()) {
// captcha field validation on check page failed- redisplay the check page, not the input page!
result = false;
}
return result;
}
/**
* Validation method that checks the given input fields.<p>
*
* All errors are stored in the member m_errors Map, with the input field name as key
* and the error message String as value.<p>
*
* @return true if all neccessary fields can be validated, otherwise false
*/
public boolean validate() {
if (m_isValidatedCorrect != null) {
return m_isValidatedCorrect.booleanValue();
}
boolean allOk = true;
// iterate the form fields
List fields = getFormConfiguration().getFields();
// validate each form field
for (int i = 0, n = fields.size(); i < n; i++) {
I_CmsField currentField = (I_CmsField)fields.get(i);
if (currentField == null) {
continue;
}
if (CmsCaptchaField.class.isAssignableFrom(currentField.getClass())) {
// the captcha field doesn't get validated here...
continue;
}
String validationError = currentField.validate(this);
if (CmsStringUtil.isNotEmpty(validationError)) {
getErrors().put(currentField.getName(), validationError);
allOk = false;
}
}
CmsCaptchaField captchaField = m_formConfiguration.getCaptchaField();
if (captchaField != null) {
boolean captchaFieldIsOnInputPage = getFormConfiguration().captchaFieldIsOnInputPage()
&& getFormConfiguration().isInputFormSubmitted();
boolean captchaFieldIsOnCheckPage = getFormConfiguration().captchaFieldIsOnCheckPage()
&& getFormConfiguration().isCheckPageSubmitted();
if (captchaFieldIsOnInputPage || captchaFieldIsOnCheckPage) {
if (!captchaField.validateCaptchaPhrase(this, captchaField.getValue())) {
getErrors().put(captchaField.getName(), ERROR_VALIDATION);
allOk = false;
}
}
}
m_isValidatedCorrect = Boolean.valueOf(allOk);
return allOk;
}
/**
* Creates a list of internet addresses (email) from a semicolon separated String.<p>
*
* @param mailAddresses a semicolon separated String with email addresses
* @return list of internet addresses (email)
* @throws AddressException if an email address is not correct
*/
protected List createInternetAddresses(String mailAddresses) throws AddressException {
if (CmsStringUtil.isNotEmpty(mailAddresses)) {
// at least one email address is present, generate list
StringTokenizer T = new StringTokenizer(mailAddresses, ";");
List addresses = new ArrayList(T.countTokens());
while (T.hasMoreTokens()) {
InternetAddress address = new InternetAddress(T.nextToken());
addresses.add(address);
}
return addresses;
} else {
// no address given, return empty list
return Collections.EMPTY_LIST;
}
}
/**
* Creates the output String of the submitted fields for email creation.<p>
*
* @param isHtmlMail if true, the output is formatted as HTML, otherwise as plain text
* @param isConfirmationMail if true, the text for the confirmation mail is created, otherwise the text for mail receiver
* @return the output String of the submitted fields for email creation
*/
protected String createMailTextFromFields(boolean isHtmlMail, boolean isConfirmationMail) {
List fieldValues = getFormConfiguration().getFields();
StringBuffer result = new StringBuffer(fieldValues.size() * 8);
if (isHtmlMail) {
// create html head with style definitions and body
result.append("<html><head>\n");
result.append("<style type=\"text/css\"><!--\n");
String style = getMessages().key("form.email.style.body");
if (CmsStringUtil.isNotEmpty(style)) {
result.append("body,h1,p,td { ");
result.append(style);
result.append(" }\n");
}
style = getMessages().key("form.email.style.h1");
if (CmsStringUtil.isNotEmpty(style)) {
result.append("h1 { ");
result.append(style);
result.append(" }\n");
}
style = getMessages().key("form.email.style.p");
if (CmsStringUtil.isNotEmpty(style)) {
result.append("p { ");
result.append(style);
result.append(" }\n");
}
style = getMessages().key("form.email.style.fields");
if (CmsStringUtil.isNotEmpty(style)) {
result.append("table.fields { ");
result.append(style);
result.append(" }\n");
}
style = getMessages().key("form.email.style.fieldlabel");
if (CmsStringUtil.isNotEmpty(style)) {
result.append("td.fieldlabel { ");
result.append(style);
result.append(" }\n");
}
style = getMessages().key("form.email.style.fieldvalue");
if (CmsStringUtil.isNotEmpty(style)) {
result.append("td.fieldvalue { ");
result.append(style);
result.append(" }\n");
}
style = getMessages().key("form.email.style.misc");
if (CmsStringUtil.isNotEmpty(style)) {
result.append(getMessages().key("form.email.style.misc"));
}
result.append("//--></style>\n");
result.append("</head><body>\n");
if (isConfirmationMail) {
// append the confirmation mail text
result.append(getFormConfiguration().getConfirmationMailText());
} else {
// append the email text
result.append(getFormConfiguration().getMailText());
}
result.append("<table border=\"0\" class=\"fields\">\n");
} else {
// generate simple text mail
if (isConfirmationMail) {
// append the confirmation mail text
result.append(getFormConfiguration().getConfirmationMailTextPlain());
} else {
// append the email text
result.append(getFormConfiguration().getMailTextPlain());
}
result.append("\n\n");
}
// generate output for submitted form fields
Iterator i = fieldValues.iterator();
while (i.hasNext()) {
I_CmsField current = (I_CmsField)i.next();
// dont show the letter of agreement (CmsPrivacyField)
if (isHtmlMail && !(current instanceof CmsPrivacyField)) {
// format output as HTML
result.append("<tr><td class=\"fieldlabel\">");
result.append(current.getLabel());
result.append("</td><td class=\"fieldvalue\">");
result.append(convertToHtmlValue(current.toString()));
result.append("</td></tr>\n");
} else if (!(current instanceof CmsPrivacyField)) {
// format output as plain text
result.append(current.getLabel());
result.append("\t");
result.append(current.toString());
result.append("\n");
}
}
if (isHtmlMail) {
// create html table closing tag
result.append("</table>\n");
if (!isConfirmationMail && getFormConfiguration().hasConfigurationErrors()) {
// write form configuration errors to html mail
result.append("<h1>");
result.append(getMessages().key("form.configuration.error.headline"));
result.append("</h1>\n<p>");
for (int k = 0; k < getFormConfiguration().getConfigurationErrors().size(); k++) {
result.append(getFormConfiguration().getConfigurationErrors().get(k));
result.append("<br>");
}
result.append("</p>\n");
}
// create body and html closing tags
result.append("</body></html>");
} else if (!isConfirmationMail && getFormConfiguration().hasConfigurationErrors()) {
// write form configuration errors to text mail
result.append("\n");
result.append(getMessages().key("form.configuration.error.headline"));
result.append("\n");
for (int k = 0; k < getFormConfiguration().getConfigurationErrors().size(); k++) {
result.append(getFormConfiguration().getConfigurationErrors().get(k));
result.append("\n");
}
}
return result.toString();
}
/**
* Sets the errors found when validating the form.<p>
*
* @param errors the errors found when validating the form
*/
protected void setErrors(Map errors) {
m_errors = errors;
}
/**
* Sets the form configuration.<p>
*
* @param configuration the form configuration
*/
protected void setFormConfiguration(CmsForm configuration) {
m_formConfiguration = configuration;
}
/**
* Sets if the form is displayed for the first time.<p>
* @param initial true if the form is displayed for the first time, otherwise false
*/
protected void setInitial(boolean initial) {
m_initial = initial;
}
/**
* Sets the localized messages.<p>
*
* @param messages the localized messages
*/
protected void setMessages(CmsMessages messages) {
m_messages = messages;
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?