diff options
author | Alexander Nittka <alex@nittka.de> | 2018-06-07 12:23:02 +0200 |
---|---|---|
committer | Ilia Motornyi <elmot@vaadin.com> | 2018-06-07 13:23:02 +0300 |
commit | 76230f8b1627faf2b10ffc7957a06f551ac2816c (patch) | |
tree | 36dc0d175d1130ae50ce82731af4f1765a887f97 | |
parent | 8fbf75cae8e7e7f45860c24dec6fe35feb8bd136 (diff) | |
download | vaadin-framework-76230f8b1627faf2b10ffc7957a06f551ac2816c.tar.gz vaadin-framework-76230f8b1627faf2b10ffc7957a06f551ac2816c.zip |
Remove parse error when changing from invalid to empty date text (#10771)
Fixes #10673.
3 files changed, 94 insertions, 4 deletions
diff --git a/server/src/main/java/com/vaadin/ui/AbstractDateField.java b/server/src/main/java/com/vaadin/ui/AbstractDateField.java index a4677e6ec3..841ad0392e 100644 --- a/server/src/main/java/com/vaadin/ui/AbstractDateField.java +++ b/server/src/main/java/com/vaadin/ui/AbstractDateField.java @@ -107,14 +107,19 @@ public abstract class AbstractDateField<T extends Temporal & TemporalAdjuster & newDate = reconstructDateFromFields(resolutions, oldDate); } + boolean parseErrorWasSet = currentParseErrorMessage != null; hasChanges |= !Objects.equals(dateString, newDateString) - || !Objects.equals(oldDate, newDate); + || !Objects.equals(oldDate, newDate) + || parseErrorWasSet; if (hasChanges) { dateString = newDateString; currentParseErrorMessage = null; if (newDateString == null || newDateString.isEmpty()) { - setValue(newDate, true); + boolean valueChanged = setValue(newDate, true); + if(!valueChanged && parseErrorWasSet) { + doSetValue(newDate); + } } else { // invalid date string if (resolutions.isEmpty()) { diff --git a/server/src/test/java/com/vaadin/tests/server/component/datefield/DateFieldErrorMessageTest.java b/server/src/test/java/com/vaadin/tests/server/component/datefield/DateFieldErrorMessageTest.java new file mode 100644 index 0000000000..2172a6f6b8 --- /dev/null +++ b/server/src/test/java/com/vaadin/tests/server/component/datefield/DateFieldErrorMessageTest.java @@ -0,0 +1,82 @@ +/* + * Copyright 2000-2018 Vaadin Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ +package com.vaadin.tests.server.component.datefield; + +import java.lang.reflect.Field; +import java.time.LocalDate; +import java.util.HashMap; +import java.util.Map; + +import org.junit.Test; + +import com.vaadin.shared.ui.datefield.AbstractDateFieldServerRpc; +import com.vaadin.shared.ui.datefield.DateResolution; +import com.vaadin.tests.server.component.abstractdatefield.AbstractLocalDateFieldDeclarativeTest; +import com.vaadin.ui.AbstractDateField; +import com.vaadin.ui.InlineDateField; + +/** + * Tests the resetting of component error after setting empty date string in + * {@link AbstractDateField}. + */ +public class DateFieldErrorMessageTest + extends AbstractLocalDateFieldDeclarativeTest<InlineDateField> { + + @Test + public void testErrorMessageRemoved() throws Exception { + InlineDateField field = new InlineDateField("Day is", + LocalDate.of(2003, 2, 27)); + checkValueAndComponentError(field, "2003-02-27", LocalDate.of(2003, 2, 27), false); + checkValueAndComponentError(field, "", null, false); + checkValueAndComponentError(field, "2003-04-27", LocalDate.of(2003, 4, 27), false); + checkValueAndComponentError(field, "foo", null, true); + checkValueAndComponentError(field, "2013-07-03", LocalDate.of(2013, 7, 3), false); + checkValueAndComponentError(field, "foo", null, true); + checkValueAndComponentError(field, "", null, false); + } + + @Override + protected String getComponentTag() { + return "vaadin-inline-date-field"; + } + + @Override + protected Class<? extends InlineDateField> getComponentClass() { + return InlineDateField.class; + } + + private void checkValueAndComponentError(InlineDateField field, String newInput, LocalDate expectedFieldValue, boolean componentErrorExpected) throws Exception { + setDateByText(field, newInput); + assertEquals(expectedFieldValue, field.getValue()); + assertEquals(componentErrorExpected, field.getComponentError()!=null); + } + + private void setDateByText(InlineDateField field, String dateText) throws Exception { + Field rcpField = AbstractDateField.class.getDeclaredField("rpc"); + rcpField.setAccessible(true); + AbstractDateFieldServerRpc rcp = (AbstractDateFieldServerRpc)rcpField.get(field); + Map<String, Integer> resolutions=new HashMap<String, Integer>(); + try { + LocalDate date = LocalDate.parse(dateText); + resolutions.put(DateResolution.YEAR.name(), date.getYear()); + resolutions.put(DateResolution.MONTH.name(), date.getMonthValue()); + resolutions.put(DateResolution.DAY.name(), date.getDayOfMonth()); + }catch (Exception e) { + //ignore + } + rcp.update(dateText, resolutions); + } +}
\ No newline at end of file diff --git a/uitest/src/test/java/com/vaadin/tests/components/datefield/DateFieldIsValidTest.java b/uitest/src/test/java/com/vaadin/tests/components/datefield/DateFieldIsValidTest.java index 9934013c78..3797dd98c4 100644 --- a/uitest/src/test/java/com/vaadin/tests/components/datefield/DateFieldIsValidTest.java +++ b/uitest/src/test/java/com/vaadin/tests/components/datefield/DateFieldIsValidTest.java @@ -37,10 +37,13 @@ public class DateFieldIsValidTest extends MultiBrowserTest { assertLogText("4. buttonClick: value: null, is valid: false"); dateTextbox.clear(); + button.click(); + assertLogText("5. buttonClick: value: null, is valid: true"); + dateTextbox.sendKeys("02/02/02", Keys.TAB); - assertLogText("5. valueChange: value: 02/02/02, is valid: true"); + assertLogText("6. valueChange: value: 02/02/02, is valid: true"); button.click(); - assertLogText("6. buttonClick: value: 02/02/02, is valid: true"); + assertLogText("7. buttonClick: value: 02/02/02, is valid: true"); } private void assertLogText(String expected) throws Exception { |