Skip to content

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

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -169,9 +169,9 @@ public class DatePicker
private Validator<LocalDate> defaultValidator = (value, context) -> {
boolean fromComponent = context == null;

if (unparsableValue != null && fallbackParserErrorMessage != null) {
if (isInputUnparsable() && fallbackParserErrorMessage != null) {
return ValidationResult.error(fallbackParserErrorMessage);
} else if (unparsableValue != null) {
} else if (isInputUnparsable()) {
return ValidationResult.error(getI18nErrorMessage(
DatePickerI18n::getBadInputErrorMessage));
}
Expand Down Expand Up @@ -668,14 +668,28 @@ private void fireValidationStatusChangeEvent() {

/**
* Returns whether the input element has a value or not.
* <p>
* For internal use only.
*
* @return <code>true</code> if the input element's value is populated,
* <code>false</code> otherwise
* @deprecated Since v24.8
*/
@Deprecated(since = "24.8")
protected boolean isInputValuePresent() {
return !getInputElementValue().isEmpty();
}

/**
* Returns whether the input value is unparsable.
*
* @return <code>true</code> if the input element's value is populated and
* unparsable, <code>false</code> otherwise
*/
protected boolean isInputUnparsable() {
return unparsableValue != null;
}

/**
* Gets the value of the input element. This value is updated on the server
* when the web component dispatches a `change` or `unparsable-change`
Expand Down Expand Up @@ -762,7 +776,7 @@ private Result<LocalDate> runFallbackParser(String s) {
@Override
public void setValue(LocalDate value) {
LocalDate oldValue = getValue();
if (oldValue == null && value == null && unparsableValue != null) {
if (oldValue == null && value == null && isInputUnparsable()) {
// When the value is programmatically cleared while the field
// contains an unparsable input, ValueChangeEvent isn't fired,
// so we need to call setModelValue manually to clear the bad
Expand Down Expand Up @@ -797,7 +811,7 @@ protected void setModelValue(LocalDate newModelValue, boolean fromClient) {
try {
isFallbackParserRunning = true;

if (fallbackParser != null && unparsableValue != null) {
if (fallbackParser != null && isInputUnparsable()) {
Result<LocalDate> result = runFallbackParser(unparsableValue);
if (result.isError()) {
fallbackParserErrorMessage = result.getMessage()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,17 @@ public class BasicValidationPage
public static final String CLEAR_VALUE_BUTTON = "clear-value-button";

public static final String REQUIRED_ERROR_MESSAGE = "Field is required";
public static final String BAD_INPUT_ERROR_MESSAGE = "Value has incorrect format";
public static final String MIN_ERROR_MESSAGE = "Value is too small";
public static final String MAX_ERROR_MESSAGE = "Value is too big";
public static final String BAD_INPUT_ERROR_MESSAGE = "Invalid date format";
public static final String INCOMPLETE_INPUT_ERROR_MESSAGE = "Must fill in both date and time";
public static final String MIN_ERROR_MESSAGE = "Date is too early";
public static final String MAX_ERROR_MESSAGE = "Date is too late";

public BasicValidationPage() {
super();

testField.setI18n(new DateTimePicker.DateTimePickerI18n()
.setRequiredErrorMessage(REQUIRED_ERROR_MESSAGE)
.setIncompleteInputErrorMessage(INCOMPLETE_INPUT_ERROR_MESSAGE)
.setBadInputErrorMessage(BAD_INPUT_ERROR_MESSAGE)
.setMinErrorMessage(MIN_ERROR_MESSAGE)
.setMaxErrorMessage(MAX_ERROR_MESSAGE));
Expand All @@ -63,6 +65,12 @@ public BasicValidationPage() {
}

protected DateTimePicker createTestField() {
return new DateTimePicker();
return new DateTimePicker() {
@Override
protected void validate() {
super.validate();
incrementServerValidationCounter();
}
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public class BinderValidationPage

public static final String REQUIRED_ERROR_MESSAGE = "Field is required";
public static final String BAD_INPUT_ERROR_MESSAGE = "Value has incorrect format";
public static final String INCOMPLETE_INPUT_ERROR_MESSAGE = "Value is incomplete";
public static final String MIN_ERROR_MESSAGE = "Value is too small";
public static final String MAX_ERROR_MESSAGE = "Value is too big";
public static final String UNEXPECTED_VALUE_ERROR_MESSAGE = "Value does not match the expected value";
Expand Down Expand Up @@ -63,6 +64,7 @@ public BinderValidationPage() {

testField.setI18n(new DateTimePicker.DateTimePickerI18n()
.setBadInputErrorMessage(BAD_INPUT_ERROR_MESSAGE)
.setIncompleteInputErrorMessage(INCOMPLETE_INPUT_ERROR_MESSAGE)
.setMinErrorMessage(MIN_ERROR_MESSAGE)
.setMaxErrorMessage(MAX_ERROR_MESSAGE));

Expand All @@ -88,6 +90,12 @@ public BinderValidationPage() {
}

protected DateTimePicker createTestField() {
return new DateTimePicker();
return new DateTimePicker() {
@Override
protected void validate() {
super.validate();
incrementServerValidationCounter();
}
};
}
}
Loading