From eb7a201f221c84c4e49d5511128c8677a6bd2687 Mon Sep 17 00:00:00 2001 From: Tobias Mattsson Date: Mon, 16 May 2011 14:06:27 +0000 Subject: [PATCH] MGNLFORM-79: Backport of r45141 ; and also applied API compatibility patch (see issue) --- .../paragraphs/models/DefaultFormDataBinder.java | 40 +++++++++-- .../module/form/validators/ValidationResult.java | 75 ++++++++++++++++++++ .../magnolia/module/form/validators/Validator.java | 13 ++++ 3 files changed, 123 insertions(+), 5 deletions(-) create mode 100644 src/main/java/info/magnolia/module/form/validators/ValidationResult.java diff --git a/src/main/java/info/magnolia/module/form/paragraphs/models/DefaultFormDataBinder.java b/src/main/java/info/magnolia/module/form/paragraphs/models/DefaultFormDataBinder.java index 8a23368..c217b83 100644 --- a/src/main/java/info/magnolia/module/form/paragraphs/models/DefaultFormDataBinder.java +++ b/src/main/java/info/magnolia/module/form/paragraphs/models/DefaultFormDataBinder.java @@ -34,6 +34,7 @@ package info.magnolia.module.form.paragraphs.models; import java.util.Iterator; +import java.util.Locale; import javax.jcr.RepositoryException; import org.apache.commons.lang.StringUtils; @@ -42,12 +43,14 @@ import info.magnolia.cms.core.Content; import info.magnolia.cms.i18n.I18nContentSupportFactory; import info.magnolia.cms.i18n.Messages; import info.magnolia.cms.i18n.MessagesManager; +import info.magnolia.cms.i18n.MessagesUtil; import info.magnolia.cms.util.NodeDataUtil; import info.magnolia.context.MgnlContext; import info.magnolia.module.form.FormModule; import info.magnolia.module.form.engine.FormDataBinder; import info.magnolia.module.form.engine.FormField; import info.magnolia.module.form.engine.FormStepState; +import info.magnolia.module.form.validators.ValidationResult; import info.magnolia.module.form.validators.Validator; /** @@ -111,8 +114,11 @@ public class DefaultFormDataBinder implements FormDataBinder { String validatorName = node.getNodeData("validation").getString(); Validator validator = FormModule.getInstance().getValidatorByName(validatorName); - if (validator != null && !validator.validate(value)) { - field.setErrorMessage(getErrorMessage(validator.getName(), node)); + if (validator != null) { + ValidationResult validationResult = validator.validateWithResult(value); + if (!validationResult.isSuccess()) { + field.setErrorMessage(getValidatorErrorMessage(validator, validationResult, node)); + } } } else if (node.hasContent(CONTENT_NAME_TEXT_FIELD_GROUP)) { Iterator textFieldGroup = node.getContent(CONTENT_NAME_TEXT_FIELD_GROUP).getChildren().iterator(); @@ -126,10 +132,34 @@ public class DefaultFormDataBinder implements FormDataBinder { return NodeDataUtil.getBoolean(node, "mandatory", false); } + private String getValidatorErrorMessage(Validator validator, ValidationResult validationResult, Content node) { + + // If the validator returned an error message will use it, possible with a resource bundle configured on the validator itself + if (StringUtils.isNotEmpty(validationResult.getErrorMessage())) { + return getErrorMessage(validationResult.getErrorMessage(), "invalid input", node, validator.getI18nBasename()); + } + + // Otherwise we'll default to a key of format: form.user.errorMessage. + return getErrorMessage(validator.getName(), node); + } + protected String getErrorMessage(String message, Content node) { - Messages messages = MessagesManager.getMessages(i18nBasename, I18nContentSupportFactory.getI18nSupport().getLocale()); - Messages defaultMessages = MessagesManager.getMessages(getDefaultPath()); - String errorMessage = messages.getWithDefault("form.user.errorMessage." + message, defaultMessages.getWithDefault("form.user.errorMessage." + message, "invalid input")); + return getErrorMessage("form.user.errorMessage." + message, "invalid input", node, null); + } + + private String getErrorMessage(String key, String defaultMsg, Content node, String overridingResourceBundle) { + + Locale locale = I18nContentSupportFactory.getI18nSupport().getLocale(); + + Messages messages = MessagesManager.getMessages(getDefaultPath()); + + messages = MessagesUtil.chain(MessagesManager.getMessages(i18nBasename, locale), messages); + + if (overridingResourceBundle != null) { + messages = MessagesUtil.chain(MessagesManager.getMessages(overridingResourceBundle, locale), messages); + } + + String errorMessage = messages.getWithDefault(key, defaultMsg); String title = node.getNodeData("title").getString(); return title + ": " + errorMessage; } diff --git a/src/main/java/info/magnolia/module/form/validators/ValidationResult.java b/src/main/java/info/magnolia/module/form/validators/ValidationResult.java new file mode 100644 index 0000000..609b1df --- /dev/null +++ b/src/main/java/info/magnolia/module/form/validators/ValidationResult.java @@ -0,0 +1,75 @@ +/** + * This file Copyright (c) 2011 Magnolia International + * Ltd. (http://www.magnolia-cms.com). All rights reserved. + * + * + * This file is dual-licensed under both the Magnolia + * Network Agreement and the GNU General Public License. + * You may elect to use one or the other of these licenses. + * + * This file is distributed in the hope that it will be + * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the + * implied warranty of MERCHANTABILITY or FITNESS FOR A + * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT. + * Redistribution, except as permitted by whichever of the GPL + * or MNA you select, is prohibited. + * + * 1. For the GPL license (GPL), you can redistribute and/or + * modify this file under the terms of the GNU General + * Public License, Version 3, as published by the Free Software + * Foundation. You should have received a copy of the GNU + * General Public License, Version 3 along with this program; + * if not, write to the Free Software Foundation, Inc., 51 + * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * 2. For the Magnolia Network Agreement (MNA), this file + * and the accompanying materials are made available under the + * terms of the MNA which accompanies this distribution, and + * is available at http://www.magnolia-cms.com/mna.html + * + * Any modifications to this file must keep this entire header + * intact. + * + */ +package info.magnolia.module.form.validators; + +/** + * Represents the result of a validation operation. + * + * @version $Id$ + * @see Validator + */ +public class ValidationResult { + + private boolean success; + private String errorMessage; + + public ValidationResult(boolean success) { + this.success = success; + } + + public ValidationResult(boolean success, String errorMessage) { + this.success = success; + this.errorMessage = errorMessage; + } + + public boolean isSuccess() { + return success; + } + + public void setSuccess(boolean success) { + this.success = success; + } + + public String getErrorMessage() { + return errorMessage; + } + + public void setErrorMessage(String errorMessage) { + this.errorMessage = errorMessage; + } + + public static ValidationResult valueOf(boolean successful) { + return new ValidationResult(successful); + } +} diff --git a/src/main/java/info/magnolia/module/form/validators/Validator.java b/src/main/java/info/magnolia/module/form/validators/Validator.java index 96d4a40..ae30381 100644 --- a/src/main/java/info/magnolia/module/form/validators/Validator.java +++ b/src/main/java/info/magnolia/module/form/validators/Validator.java @@ -43,11 +43,16 @@ package info.magnolia.module.form.validators; public class Validator { private String name; + private String i18nBasename; public boolean validate(String value) { return true; } + public ValidationResult validateWithResult(String value) { + return new ValidationResult(validate(value)); + } + public String getName() { return name; } @@ -55,4 +60,12 @@ public class Validator { public void setName(String name) { this.name = name; } + + public String getI18nBasename() { + return i18nBasename; + } + + public void setI18nBasename(String i18nBasename) { + this.i18nBasename = i18nBasename; + } } -- 1.7.4