From eecf11ad18484d5a3862aa6fedcc51fdffc8b4eb Mon Sep 17 00:00:00 2001 From: Artur Signell Date: Mon, 19 Dec 2011 14:07:48 +0200 Subject: [PATCH] #8102 Allow overriding the conversion error message for Fields --- src/com/vaadin/ui/AbstractField.java | 63 ++++++++++++++++++++++++---- 1 file changed, 55 insertions(+), 8 deletions(-) diff --git a/src/com/vaadin/ui/AbstractField.java b/src/com/vaadin/ui/AbstractField.java index 2a7f0e42d3..339741545d 100644 --- a/src/com/vaadin/ui/AbstractField.java +++ b/src/com/vaadin/ui/AbstractField.java @@ -135,6 +135,11 @@ public abstract class AbstractField extends AbstractComponent implements */ private String requiredError = ""; + /** + * The error message that is shown when the field value cannot be converted. + */ + private String valueConversionError = "Could not convert value to {0}"; + /** * Is automatic validation enabled. */ @@ -848,8 +853,13 @@ public abstract class AbstractField extends AbstractComponent implements * If there is a value converter, always use it. It must convert or * throw an exception. */ - return valueConverter.convertFromTargetToSource(fieldValue, - getLocale()); + try { + return valueConverter.convertFromTargetToSource(fieldValue, + getLocale()); + } catch (com.vaadin.data.util.converter.Converter.ConversionException e) { + throw new ConversionException( + getValueConversionError(valueConverter.getSourceType())); + } } if (fieldValue == null) { @@ -871,12 +881,24 @@ public abstract class AbstractField extends AbstractComponent implements return fieldValue; } else { throw new Converter.ConversionException( - "Unable to convert value of type " - + fieldValue.getClass().getName() - + " to " - + type.getName() - + ". No value converter is set and the types are not compatible."); + getValueConversionError(type)); + } + } + /** + * Returns the value conversion error with {0} replaced by the data source + * type. + * + * @param dataSourceType + * The type of the data source + * @return The value conversion error string with parameters replaced. + */ + protected String getValueConversionError(Class dataSourceType) { + if (dataSourceType == null) { + return getValueConversionError(); + } else { + return getValueConversionError().replace("{0}", + dataSourceType.getSimpleName()); } } @@ -998,7 +1020,9 @@ public abstract class AbstractField extends AbstractComponent implements valueToValidate = getValueConverter() .convertFromTargetToSource(fieldValue, getLocale()); } catch (Exception e) { - throw new InvalidValueException(e.getMessage()); + throw new InvalidValueException( + getValueConversionError(getValueConverter() + .getSourceType())); } } @@ -1390,6 +1414,29 @@ public abstract class AbstractField extends AbstractComponent implements return requiredError; } + /** + * Gets the error that is shown if the field value cannot be converted to + * the data source type. + * + * @return The error that is shown if conversion of the field value fails + */ + public String getValueConversionError() { + return valueConversionError; + } + + /** + * Sets the error that is shown if the field value cannot be converted to + * the data source type. If {0} is present in the message, it will be + * replaced by the simple name of the data source type. + * + * @param valueConversionError + * Message to be shown when conversion of the value fails + */ + public void setValueConversionError(String valueConversionError) { + this.valueConversionError = valueConversionError; + requestRepaint(); + } + /** * Is the field empty? * -- 2.39.5