Skip to content

Commit

Permalink
refactor!: put getDefaultValidator implementation behind a feature flag
Browse files Browse the repository at this point in the history
  • Loading branch information
vursen committed Aug 28, 2022
1 parent 8a6c832 commit 8b31acf
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*/
package com.vaadin.flow.component.textfield;

import com.vaadin.experimental.Feature;
import com.vaadin.experimental.FeatureFlags;
import com.vaadin.flow.component.AttachEvent;
import com.vaadin.flow.component.CompositionNotifier;
import com.vaadin.flow.component.shared.HasAllowedCharPattern;
Expand All @@ -31,6 +33,7 @@
import com.vaadin.flow.data.binder.Validator;
import com.vaadin.flow.data.value.HasValueChangeMode;
import com.vaadin.flow.data.value.ValueChangeMode;
import com.vaadin.flow.server.VaadinService;

/**
* Text Field allows the user to input and edit text. Prefix and suffix
Expand Down Expand Up @@ -487,7 +490,12 @@ public void setRequiredIndicatorVisible(boolean requiredIndicatorVisible) {

@Override
public Validator<String> getDefaultValidator() {
return (value, context) -> getValidationSupport().checkValidity(value);
if (isFeatureFlagEnabled(FeatureFlags.ENFORCE_FIELD_VALIDATION)) {
return (value, context) -> getValidationSupport()
.checkValidity(value);
}

return null;
}

/**
Expand Down Expand Up @@ -520,4 +528,25 @@ public void addThemeVariants(TextFieldVariant... variants) {
public void removeThemeVariants(TextFieldVariant... variants) {
HasThemeVariant.super.removeThemeVariants(variants);
}

/**
* Returns true if the given feature flag is enabled, false otherwise.
* <p>
* Exposed with protected visibility to support mocking
* <p>
* The method requires the {@code VaadinService} instance to obtain the
* available feature flags, otherwise, the feature is considered disabled.
*
* @param feature
* the feature flag.
* @return whether the feature flag is enabled.
*/
protected boolean isFeatureFlagEnabled(Feature feature) {
VaadinService service = VaadinService.getCurrent();
if (service == null) {
return false;
}

return FeatureFlags.get(service.getContext()).isEnabled(feature);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.vaadin.flow.component.textfield.binder;

import com.vaadin.experimental.Feature;
import com.vaadin.experimental.FeatureFlags;
import com.vaadin.flow.component.Tag;
import com.vaadin.flow.component.textfield.TextField;
import com.vaadin.flow.function.SerializablePredicate;

Expand All @@ -8,9 +11,20 @@
public class TextFieldValidationTest
extends AbstractTextFieldValidationTest<String, TextField> {

@Override
@Tag("test-text-field")
private class TestTextField extends TextField {
protected boolean isFeatureFlagEnabled(Feature feature) {
if (feature.getId() == FeatureFlags.ENFORCE_FIELD_VALIDATION
.getId()) {
return true;
}

return super.isFeatureFlagEnabled(feature);
}
}

protected void initField() {
field = new TextField();
field = new TestTextField();
field.setMaxLength(8);
}

Expand Down

0 comments on commit 8b31acf

Please sign in to comment.