diff options
author | Sun Zhe <31067185+ZheSun88@users.noreply.github.com> | 2018-11-13 13:48:28 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-11-13 13:48:28 +0200 |
commit | dd0b0f44c4d9639fe6db60fe987eef0c4d9e36b7 (patch) | |
tree | b580007e921306c10ae2c25a9dfb06bf22aa92fd /server | |
parent | 26a2ffd8e467598c7ca388df4130f7532999cf9b (diff) | |
download | vaadin-framework-dd0b0f44c4d9639fe6db60fe987eef0c4d9e36b7.tar.gz vaadin-framework-dd0b0f44c4d9639fe6db60fe987eef0c4d9e36b7.zip |
Correct the logic for validation used in DateField (#11307)
Fixes #11276
Refactor the code and correct the logic for validation
Rename an internal variable, as it contains more than one type of messages
Diffstat (limited to 'server')
-rw-r--r-- | server/src/main/java/com/vaadin/ui/AbstractDateField.java | 47 |
1 files changed, 33 insertions, 14 deletions
diff --git a/server/src/main/java/com/vaadin/ui/AbstractDateField.java b/server/src/main/java/com/vaadin/ui/AbstractDateField.java index 48bc3e13f5..f03c900b97 100644 --- a/server/src/main/java/com/vaadin/ui/AbstractDateField.java +++ b/server/src/main/java/com/vaadin/ui/AbstractDateField.java @@ -38,10 +38,9 @@ import java.util.logging.Logger; import java.util.stream.Collectors; import java.util.stream.Stream; -import elemental.json.Json; +import com.googlecode.gentyref.GenericTypeReflector; import org.jsoup.nodes.Element; -import com.googlecode.gentyref.GenericTypeReflector; import com.vaadin.data.Result; import com.vaadin.data.ValidationResult; import com.vaadin.data.Validator; @@ -64,6 +63,8 @@ import com.vaadin.ui.declarative.DesignAttributeHandler; import com.vaadin.ui.declarative.DesignContext; import com.vaadin.util.TimeZoneUtil; +import elemental.json.Json; + /** * A date editor component with {@link LocalDate} as an input value. * @@ -107,14 +108,14 @@ public abstract class AbstractDateField<T extends Temporal & TemporalAdjuster & newDate = reconstructDateFromFields(resolutions, oldDate); } - boolean parseErrorWasSet = currentParseErrorMessage != null; + boolean parseErrorWasSet = currentErrorMessage != null; hasChanges |= !Objects.equals(dateString, newDateString) || !Objects.equals(oldDate, newDate) || parseErrorWasSet; if (hasChanges) { dateString = newDateString; - currentParseErrorMessage = null; + currentErrorMessage = null; if (newDateString == null || newDateString.isEmpty()) { boolean valueChanged = setValue(newDate, true); if (!valueChanged && parseErrorWasSet) { @@ -138,8 +139,8 @@ public abstract class AbstractDateField<T extends Temporal & TemporalAdjuster & }); if (parsedDate.isError()) { dateString = null; - currentParseErrorMessage = parsedDate - .getMessage().orElse("Parsing error"); + currentErrorMessage = parsedDate.getMessage() + .orElse("Parsing error"); if (!isDifferentValue(null)) { doSetValue(null); @@ -187,7 +188,7 @@ public abstract class AbstractDateField<T extends Temporal & TemporalAdjuster & private String dateString = ""; - private String currentParseErrorMessage; + private String currentErrorMessage; private String defaultParseErrorMessage = "Date format not recognized"; @@ -607,7 +608,7 @@ public abstract class AbstractDateField<T extends Temporal & TemporalAdjuster & */ @Override public void setValue(T value) { - currentParseErrorMessage = null; + currentErrorMessage = null; /* * First handle special case when the client side component have a date * string but value is null (e.g. unparsable date string typed in by the @@ -783,16 +784,16 @@ public abstract class AbstractDateField<T extends Temporal & TemporalAdjuster & new ValueContext(this, this)); if (result.isError()) { - currentParseErrorMessage = getDateOutOfRangeMessage(); + currentErrorMessage = getDateOutOfRangeMessage(); } - getState().parsable = currentParseErrorMessage == null; + getState().parsable = currentErrorMessage == null; ErrorMessage errorMessage; - if (currentParseErrorMessage == null) { + if (currentErrorMessage == null) { errorMessage = null; } else { - errorMessage = new UserError(currentParseErrorMessage); + errorMessage = new UserError(currentErrorMessage); } setComponentError(errorMessage); @@ -876,9 +877,27 @@ public abstract class AbstractDateField<T extends Temporal & TemporalAdjuster & return new Validator<T>() { @Override public ValidationResult apply(T value, ValueContext context) { - if (currentParseErrorMessage != null) { - return ValidationResult.error(currentParseErrorMessage); + + // currentErrorMessage contains two type of messages, one is + // DateOutOfRangeMessage and the other one is the ParseError + if (currentErrorMessage != null) { + if (currentErrorMessage + .equals(getDateOutOfRangeMessage())) { + // if the currentErrorMessage is DateOutOfRangeMessage, + // then need to double check whether the error message + // has been updated, that is because of #11276. + ValidationResult validationResult = getRangeValidator() + .apply(value, context); + if (validationResult.isError()) { + return ValidationResult.error(currentErrorMessage); + } + } else { + // if the current Error is parsing error, pass it to the + // ValidationResult + return ValidationResult.error(currentErrorMessage); + } } + // Pass to range validator. return getRangeValidator().apply(value, context); } |