-
Notifications
You must be signed in to change notification settings - Fork 70
feat: date time picker validation improvements #7346
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
2a8f775
to
548332e
Compare
// Notify the server in order to use the formatted values in validation | ||
pendingInputElementValueSyncs++; | ||
getElement().executeJs( | ||
"this.dispatchEvent(new CustomEvent('value-programmatically-set'));"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you please explain why this additional event is necessary? What scenario is it intended to cover?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This approach might not be correct since isInvalid()
will still report true
on the server side until the next round-trip occurs. I explored this a bit on my own and may have found a solution that avoids a round-trip: cbd6285. Please let me know if you see any issues with it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This solution is quite good overall. Updated the PR based in this approach and applied a few fixes.
4faeebe
to
0fc0548
Compare
@Test | ||
public void setValueProgrammatically_fieldValidatedOnce() { | ||
clickElementWithJs(SET_VALUE_PROGRAMMATICALLY); | ||
assertValidationCount(1); | ||
} | ||
|
||
@Test | ||
public void clearAndSetValueProgrammatically_fieldValidatedTwice() { | ||
clickElementWithJs(CLEAR_AND_SET_VALUE_PROGRAMMATICALLY); | ||
assertValidationCount(2); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This could be tested using unit tests in datetimepicker/validation/BasicValidationTest.java
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removed from ITs and converted to unit tests.
public void triggerBlurWithNoChange_fieldNotValidated() { | ||
dateInput.sendKeys(Keys.TAB); | ||
timeInput.sendKeys(Keys.TAB); | ||
assertValidationCount(0); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I suggest combining the tests that assert validity with those that assert the validation count. The validation count should be asserted in every test that verifies validity
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added validation count assertions.
&& value == null | ||
&& (isInputUnparsable() || isInputIncomplete()); | ||
synchronizeChildComponentValues(value); | ||
validate(shouldFireValidationStatusChangeEvent); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Components must be validated before user-defined ValueChangeEvent
listeners are triggered to ensure that those listeners have access to the updated invalid state. That's why validate()
was originally called inside addValueChangeListener
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changing the order seems to handle this issue. Added a unit test that should cover this case.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure... it looks like validation is now running with the previous value.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You are absolutely right. The test passing was a coincidence. I reverted the change
and replaced change event listener with value change listener.
@@ -327,6 +353,11 @@ public DateTimePicker(LocalDateTime initialDateTime, Locale locale) { | |||
setLocale(locale); | |||
} | |||
|
|||
private void addValidationListeners() { | |||
getElement().addEventListener("change", e -> validate(true)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Calling validate(true)
on change will result in duplicate validation when using Binder, since Binder already listens to ValueChangeEvent
and triggers validation automatically. To prevent similar regressions in the future, each test in BinderValidationIT
should assert the validation count.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The validation count seems to be in line with regular validation in my testing. Can you provide some example cases where the binder validation count is doubled?
@@ -193,7 +211,7 @@ public void setValue_clearValue_assertValidity() { | |||
|
|||
@Test | |||
public void badInput_changeValue_assertValidity() { | |||
setInputValue(dateInput, "INVALID"); | |||
setFieldInvalid(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's use explicit setInputValue
calls throughout to avoid potential side-effects and make the test flow more obvious.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated to set the values explicitly.
assertErrorMessage(REQUIRED_ERROR_MESSAGE); | ||
assertServerValid(); | ||
assertClientValid(); | ||
assertErrorMessage(null); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Each test case should also assert the validation count.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added count assertions.
000387f
to
cb34131
Compare
cb34131
to
e8df2b8
Compare
|
Description
This PR implements DateTimePicker validation improvements for the Flow counterpart. The changes include:
unparsable-change
event from the web componentvalidated
eventBased on the prototype.
Depends on vaadin/web-components#8986.
Part of #6697
Type of change
Checklist