Skip to content

Commit

Permalink
fix: check for input value populated state
Browse files Browse the repository at this point in the history
  • Loading branch information
yuriy-fix committed Aug 4, 2022
1 parent 8ea97ef commit 417a5bf
Showing 1 changed file with 19 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

import com.vaadin.flow.component.AttachEvent;
import com.vaadin.flow.component.ComponentEventListener;
import com.vaadin.flow.component.Synchronize;
import com.vaadin.flow.component.shared.ClientValidationUtil;
import com.vaadin.flow.component.shared.HasAllowedCharPattern;
import com.vaadin.flow.component.shared.HasClearButton;
Expand Down Expand Up @@ -94,7 +95,6 @@ public class DatePicker extends GeneratedVaadinDatePicker<DatePicker, LocalDate>
private LocalDate max;
private LocalDate min;
private boolean required;
private boolean isClientInvalid;
private final Collection<ValidationStatusChangeListener<LocalDate>> validationStatusChangeListeners = new ArrayList<>();

private StateTree.ExecutionRegistration pendingI18nUpdate;
Expand Down Expand Up @@ -143,7 +143,6 @@ private DatePicker(LocalDate initialDate, boolean isInitialValueOptional) {
setInvalid(false);

addClientValidatedEventListener(e -> {
isClientInvalid = !e.isValid();
validate();
fireValidationStatusChangeEvent();
});
Expand Down Expand Up @@ -509,6 +508,12 @@ public boolean isInvalid() {
}

private ValidationResult checkValidity(LocalDate value) {
var hasNonParsableValue = value == getEmptyValue()
&& getInputValuePopulated();
if (hasNonParsableValue) {
return ValidationResult.error("");
}

var greaterThanMax = ValidationUtil.checkGreaterThanMax(value, max);
if (greaterThanMax.isError()) {
return greaterThanMax;
Expand All @@ -519,10 +524,6 @@ private ValidationResult checkValidity(LocalDate value) {
return smallerThanMin;
}

if (isClientInvalid) {
return ValidationResult.error("");
}

return ValidationResult.ok();
}

Expand All @@ -538,6 +539,18 @@ private boolean isInvalid(LocalDate value) {
return requiredValidation.isError() || checkValidity(value).isError();
}

/**
* Gets the populated state of the input's value, which is {@code false} by
* default.
*
* @return <code>true</code> if the input's value is populated,
* <code>false</code> otherwise
*/
@Synchronize(property = "_hasInputValue", value = "has-input-value-changed")
private boolean getInputValuePopulated() {
return getElement().getProperty("_hasInputValue", false);
}

/**
* Sets the label for the datepicker.
*
Expand Down

0 comments on commit 417a5bf

Please sign in to comment.