This repository has been archived by the owner on Apr 6, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Invalidate non-parseable date string
- sync input value from client to server - use input value in server-side validation - validate on blur in addition to value change event fix #223
- Loading branch information
Showing
6 changed files
with
328 additions
and
28 deletions.
There are no files selected for viewing
26 changes: 26 additions & 0 deletions
26
...ation-tests/src/main/java/com/vaadin/flow/component/datepicker/InvalidDateStringView.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package com.vaadin.flow.component.datepicker; | ||
|
||
import com.vaadin.flow.component.html.Div; | ||
import com.vaadin.flow.component.html.NativeButton; | ||
import com.vaadin.flow.router.Route; | ||
|
||
@Route("invalid-date-string") | ||
public class InvalidDateStringView extends Div { | ||
|
||
public InvalidDateStringView() { | ||
DatePicker datePicker = new DatePicker(); | ||
|
||
Div value = new Div(); | ||
datePicker.addValueChangeListener(e -> value.setText( | ||
e.getValue() == null ? "null" : e.getValue().toString())); | ||
value.setId("value"); | ||
|
||
NativeButton checkValidity = new NativeButton("check validity", | ||
e -> e.getSource() | ||
.setText(datePicker.isInvalid() ? "invalid" : "valid")); | ||
checkValidity.setId("check-validity"); | ||
|
||
add(datePicker, value, checkValidity); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
...gration-tests/src/test/java/com/vaadin/flow/component/datepicker/InvalidDateStringIT.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
/* | ||
* Copyright 2000-2017 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.flow.component.datepicker; | ||
|
||
import org.junit.Assert; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
import com.vaadin.flow.component.datepicker.testbench.DatePickerElement; | ||
import com.vaadin.flow.testutil.AbstractComponentIT; | ||
import com.vaadin.flow.testutil.TestPath; | ||
import com.vaadin.testbench.TestBenchElement; | ||
|
||
/** | ||
* Integration tests for the {@link InvalidDateStringView}. | ||
*/ | ||
@TestPath("invalid-date-string") | ||
public class InvalidDateStringIT extends AbstractComponentIT { | ||
|
||
private DatePickerElement datePicker; | ||
private TestBenchElement checkValidity; | ||
private TestBenchElement value; | ||
|
||
@Before | ||
public void init() { | ||
open(); | ||
datePicker = $(DatePickerElement.class).first(); | ||
checkValidity = $(TestBenchElement.class).id("check-validity"); | ||
value = $(TestBenchElement.class).id("value"); | ||
} | ||
|
||
@Test | ||
public void setInvalidDateString_fieldInvalid() { | ||
datePicker.setInputValue("asdf"); | ||
assertValid(false); | ||
assertValue(""); | ||
} | ||
|
||
@Test | ||
public void setValidValue_setInvalidDateString_fieldInvalid() { | ||
datePicker.setInputValue("1/1/2020"); | ||
assertValid(true); | ||
assertValue("2020-01-01"); | ||
datePicker.setInputValue("asdf"); | ||
assertValid(false); | ||
assertValue(null); | ||
} | ||
|
||
@Test | ||
public void setInvalidDateString_clearField_fieldValid() { | ||
datePicker.setInputValue("asdf"); | ||
datePicker.setInputValue(""); | ||
assertValid(true); | ||
assertValue(""); | ||
} | ||
|
||
@Test | ||
public void setInvalidDateString_setValidValue_fieldValid() { | ||
datePicker.setInputValue("asdf"); | ||
datePicker.setInputValue("1/1/2020"); | ||
assertValid(true); | ||
assertValue("2020-01-01"); | ||
} | ||
|
||
private void assertValid(boolean expectedValid) { | ||
String expectedText = expectedValid ? "valid" : "invalid"; | ||
checkValidity.click(); | ||
Assert.assertEquals("Expected DatePicker to be " + expectedText, | ||
expectedText, checkValidity.getText()); | ||
} | ||
|
||
private void assertValue(String expectedValue) { | ||
Assert.assertEquals("Unexpected DatePicker value", | ||
expectedValue == null ? "null" : expectedValue, | ||
value.getText()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.