Skip to content

Commit

Permalink
feat!: support setting error messages with TextFieldI18n
Browse files Browse the repository at this point in the history
checkRequired => validateRequiredConstraint

add unit test

polish

run formatter

docs: improve grid empty state documentation (#6351)

add more unit tests

wip
  • Loading branch information
vursen committed Jun 7, 2024
1 parent 0279e9e commit 3f8e7d5
Show file tree
Hide file tree
Showing 10 changed files with 438 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ public interface HasValidationProperties extends HasElement, HasValidation {

/**
* Sets the error message to show to the user when the component is invalid.
* <p>
* NOTE: If you need to manually control error messages, consider enabling
* manual validation mode with {@link #setManualValidation(boolean)} to
* avoid conflicts between your custom validation and the component's
* built-in validation.
*
* @param errorMessage
* the error message or {@code null} to clear it
Expand All @@ -53,8 +58,8 @@ default String getErrorMessage() {
* <p>
* NOTE: If you need to manually control the invalid state, consider
* enabling manual validation mode with
* {@link #setManualValidation(boolean)} to avoid potential conflicts
* between your custom validation and the component's built-in validation.
* {@link #setManualValidation(boolean)} to avoid conflicts between your
* custom validation and the component's built-in validation.
*
* @param invalid
* {@code true} for invalid, {@code false} for valid
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,4 +170,72 @@ public static <V extends Comparable<V>> ValidationResult validateMinConstraint(
return isError ? ValidationResult.error(errorMessage)
: ValidationResult.ok();
}

/**
* Checks if the value satisfies the minimum length constraint and returns a
* {@code ValidationResult.ok()} or {@code ValidationResult.error()} with
* the given error message depending on the result.
*
* @param errorMessage
* the error message to return if the check fails
* @param value
* the value to validate
* @param minLength
* the minimum allowed length
* @return {@code ValidationResult.ok()} if the value is shorter than or
* equal to the minimum length, {@code ValidationResult.error()}
* otherwise
*/
public static ValidationResult validateMinLengthConstraint(
String errorMessage, String value, Integer minLength) {
boolean isError = value != null && !value.isEmpty() && minLength != null
&& value.length() < minLength;
return isError ? ValidationResult.error(errorMessage)
: ValidationResult.ok();
}

/**
* Checks if the value satisfies the maximum length constraint and returns a
* {@code ValidationResult.ok()} or {@code ValidationResult.error()} with
* the given error message depending on the result.
*
* @param errorMessage
* the error message to return if the check fails
* @param value
* the value to validate
* @param maxLength
* the maximum allowed length
* @return {@code ValidationResult.ok()} if the value is longer than or
* equal to the minimum length, {@code ValidationResult.error()}
* otherwise
*/
public static ValidationResult validateMaxLengthConstraint(
String errorMessage, String value, Integer maxLength) {
boolean isError = value != null && maxLength != null
&& value.length() > maxLength;
return isError ? ValidationResult.error(errorMessage)
: ValidationResult.ok();
}

/**
* Checks if the value satisfies the pattern constraint and returns a
* {@code ValidationResult.ok()} or {@code ValidationResult.error()} with
* the given error message depending on the result.
*
* @param errorMessage
* the error message to return if the check fails
* @param value
* the value to validate
* @param pattern
* the pattern to match
* @return {@code ValidationResult.ok()} if the value matches the pattern,
* {@code ValidationResult.error()} otherwise
*/
public static ValidationResult validatePatternConstraint(
String errorMessage, String value, String pattern) {
boolean isError = value != null && !value.isEmpty() && pattern != null
&& !pattern.isEmpty() && !value.matches(pattern);
return isError ? ValidationResult.error(errorMessage)
: ValidationResult.ok();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,20 @@ public class TextFieldBasicValidationPage
public static final String MIN_LENGTH_INPUT = "min-length-input";
public static final String MAX_LENGTH_INPUT = "max-length-input";

public static final String REQUIRED_ERROR_MESSAGE = "Field is required";
public static final String MIN_LENGTH_ERROR_MESSAGE = "Value is too short";
public static final String MAX_LENGTH_ERROR_MESSAGE = "Value is too long";
public static final String PATTERN_ERROR_MESSAGE = "Value does not match the pattern";

public TextFieldBasicValidationPage() {
super();

testField.setI18n(new TextField.TextFieldI18n()
.setRequiredErrorMessage(REQUIRED_ERROR_MESSAGE)
.setMinLengthErrorMessage(MIN_LENGTH_ERROR_MESSAGE)
.setMaxLengthErrorMessage(MAX_LENGTH_ERROR_MESSAGE)
.setPatternErrorMessage(PATTERN_ERROR_MESSAGE));

add(createButton(REQUIRED_BUTTON, "Enable required", event -> {
testField.setRequired(true);
}));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,11 @@ public class TextFieldBinderValidationPage
public static final String MAX_LENGTH_INPUT = "max-length-input";
public static final String EXPECTED_VALUE_INPUT = "expected-value-input";

public static final String REQUIRED_ERROR_MESSAGE = "The field is required";
public static final String UNEXPECTED_VALUE_ERROR_MESSAGE = "The field doesn't match the expected value";
public static final String REQUIRED_ERROR_MESSAGE = "Field is required";
public static final String MIN_LENGTH_ERROR_MESSAGE = "Value is too short";
public static final String MAX_LENGTH_ERROR_MESSAGE = "Value is too long";
public static final String PATTERN_ERROR_MESSAGE = "Value does not match the pattern";
public static final String UNEXPECTED_VALUE_ERROR_MESSAGE = "Value does not match the expected value";

public static class Bean {
private String property;
Expand All @@ -50,6 +53,11 @@ public void setProperty(String property) {
public TextFieldBinderValidationPage() {
super();

testField.setI18n(new TextField.TextFieldI18n()
.setMinLengthErrorMessage(MIN_LENGTH_ERROR_MESSAGE)
.setMaxLengthErrorMessage(MAX_LENGTH_ERROR_MESSAGE)
.setPatternErrorMessage(PATTERN_ERROR_MESSAGE));

binder = new Binder<>(Bean.class);
binder.forField(testField).asRequired(REQUIRED_ERROR_MESSAGE)
.withValidator(value -> value.equals(expectedValue),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,12 @@

import static com.vaadin.flow.component.textfield.tests.validation.TextFieldBasicValidationPage.MIN_LENGTH_INPUT;
import static com.vaadin.flow.component.textfield.tests.validation.TextFieldBasicValidationPage.MAX_LENGTH_INPUT;
import static com.vaadin.flow.component.textfield.tests.validation.TextFieldBasicValidationPage.MIN_LENGTH_ERROR_MESSAGE;
import static com.vaadin.flow.component.textfield.tests.validation.TextFieldBasicValidationPage.MAX_LENGTH_ERROR_MESSAGE;
import static com.vaadin.flow.component.textfield.tests.validation.TextFieldBasicValidationPage.PATTERN_ERROR_MESSAGE;
import static com.vaadin.flow.component.textfield.tests.validation.TextFieldBasicValidationPage.PATTERN_INPUT;
import static com.vaadin.flow.component.textfield.tests.validation.TextFieldBasicValidationPage.REQUIRED_BUTTON;
import static com.vaadin.flow.component.textfield.tests.validation.TextFieldBasicValidationPage.REQUIRED_ERROR_MESSAGE;

@TestPath("vaadin-text-field/validation/basic")
public class TextFieldBasicValidationIT
Expand All @@ -34,6 +38,7 @@ public class TextFieldBasicValidationIT
public void fieldIsInitiallyValid() {
assertClientValid();
assertServerValid();
assertErrorMessage(null);
}

@Test
Expand All @@ -42,6 +47,7 @@ public void triggerBlur_assertValidity() {
assertValidationCount(0);
assertServerValid();
assertClientValid();
assertErrorMessage(null);
}

@Test
Expand All @@ -52,6 +58,7 @@ public void required_triggerBlur_assertValidity() {
assertValidationCount(0);
assertServerValid();
assertClientValid();
assertErrorMessage(null);
}

@Test
Expand All @@ -62,11 +69,13 @@ public void required_changeValue_assertValidity() {
assertValidationCount(1);
assertServerValid();
assertClientValid();
assertErrorMessage("");

testField.setValue("");
assertValidationCount(1);
assertServerInvalid();
assertClientInvalid();
assertErrorMessage(REQUIRED_ERROR_MESSAGE);
}

@Test
Expand All @@ -77,16 +86,19 @@ public void minLength_changeValue_assertValidity() {
assertValidationCount(1);
assertClientInvalid();
assertServerInvalid();
assertErrorMessage(MIN_LENGTH_ERROR_MESSAGE);

testField.setValue("AA");
assertValidationCount(1);
assertClientValid();
assertServerValid();
assertErrorMessage("");

testField.setValue("AAA");
assertValidationCount(1);
assertClientValid();
assertServerValid();
assertErrorMessage("");
}

@Test
Expand All @@ -97,16 +109,19 @@ public void maxLength_changeValue_assertValidity() {
assertValidationCount(1);
assertClientInvalid();
assertServerInvalid();
assertErrorMessage(MAX_LENGTH_ERROR_MESSAGE);

testField.setValue("AA");
assertValidationCount(1);
assertClientValid();
assertServerValid();
assertErrorMessage("");

testField.setValue("A");
assertValidationCount(1);
assertClientValid();
assertServerValid();
assertErrorMessage("");
}

@Test
Expand All @@ -117,11 +132,13 @@ public void pattern_changeValue_assertValidity() {
assertValidationCount(1);
assertClientInvalid();
assertServerInvalid();
assertErrorMessage(PATTERN_ERROR_MESSAGE);

testField.setValue("1234");
assertValidationCount(1);
assertClientValid();
assertServerValid();
assertErrorMessage("");
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@
import static com.vaadin.flow.component.textfield.tests.validation.TextFieldBinderValidationPage.MIN_LENGTH_INPUT;
import static com.vaadin.flow.component.textfield.tests.validation.TextFieldBinderValidationPage.MAX_LENGTH_INPUT;
import static com.vaadin.flow.component.textfield.tests.validation.TextFieldBinderValidationPage.EXPECTED_VALUE_INPUT;
import static com.vaadin.flow.component.textfield.tests.validation.TextFieldBinderValidationPage.MIN_LENGTH_ERROR_MESSAGE;
import static com.vaadin.flow.component.textfield.tests.validation.TextFieldBinderValidationPage.MAX_LENGTH_ERROR_MESSAGE;
import static com.vaadin.flow.component.textfield.tests.validation.TextFieldBinderValidationPage.PATTERN_ERROR_MESSAGE;
import static com.vaadin.flow.component.textfield.tests.validation.TextFieldBinderValidationPage.REQUIRED_ERROR_MESSAGE;
import static com.vaadin.flow.component.textfield.tests.validation.TextFieldBinderValidationPage.UNEXPECTED_VALUE_ERROR_MESSAGE;

Expand All @@ -45,6 +48,7 @@ public void required_triggerBlur_assertValidity() {
assertValidationCount(0);
assertServerValid();
assertClientValid();
assertErrorMessage(null);
}

@Test
Expand All @@ -55,6 +59,7 @@ public void required_changeValue_assertValidity() {
assertValidationCount(1);
assertServerValid();
assertClientValid();
assertErrorMessage("");

testField.setValue("");
assertValidationCount(1);
Expand All @@ -73,7 +78,7 @@ public void minLength_changeValue_assertValidity() {
assertValidationCount(1);
assertClientInvalid();
assertServerInvalid();
assertErrorMessage("");
assertErrorMessage(MIN_LENGTH_ERROR_MESSAGE);

// Binder validation fails:
testField.setValue("AA");
Expand All @@ -87,6 +92,7 @@ public void minLength_changeValue_assertValidity() {
assertValidationCount(1);
assertClientValid();
assertServerValid();
assertErrorMessage("");
}

@Test
Expand All @@ -99,7 +105,7 @@ public void maxLength_changeValue_assertValidity() {
assertValidationCount(1);
assertClientInvalid();
assertServerInvalid();
assertErrorMessage("");
assertErrorMessage(MAX_LENGTH_ERROR_MESSAGE);

// Binder validation fails:
testField.setValue("AA");
Expand All @@ -113,6 +119,7 @@ public void maxLength_changeValue_assertValidity() {
assertValidationCount(1);
assertClientValid();
assertServerValid();
assertErrorMessage("");
}

@Test
Expand All @@ -125,7 +132,7 @@ public void pattern_changeValue_assertValidity() {
assertValidationCount(1);
assertClientInvalid();
assertServerInvalid();
assertErrorMessage("");
assertErrorMessage(PATTERN_ERROR_MESSAGE);

// Binder validation fails:
testField.setValue("12");
Expand All @@ -139,6 +146,7 @@ public void pattern_changeValue_assertValidity() {
assertValidationCount(1);
assertClientValid();
assertServerValid();
assertErrorMessage("");
}

@Override
Expand Down
Loading

0 comments on commit 3f8e7d5

Please sign in to comment.