Skip to content
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

feat: add i18n error message support for TextField #6343

Merged
merged 11 commits into from
Jul 19, 2024
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 longer 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 shorter than or
* equal to the maximum 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 Down Expand Up @@ -73,7 +77,7 @@ public void minLength_changeValue_assertValidity() {
assertValidationCount(1);
assertClientInvalid();
assertServerInvalid();
assertErrorMessage("");
assertErrorMessage(MIN_LENGTH_ERROR_MESSAGE);

// Binder validation fails:
testField.setValue("AA");
Expand All @@ -99,7 +103,7 @@ public void maxLength_changeValue_assertValidity() {
assertValidationCount(1);
assertClientInvalid();
assertServerInvalid();
assertErrorMessage("");
assertErrorMessage(MAX_LENGTH_ERROR_MESSAGE);

// Binder validation fails:
testField.setValue("AA");
Expand All @@ -125,7 +129,7 @@ public void pattern_changeValue_assertValidity() {
assertValidationCount(1);
assertClientInvalid();
assertServerInvalid();
assertErrorMessage("");
assertErrorMessage(PATTERN_ERROR_MESSAGE);

// Binder validation fails:
testField.setValue("12");
Expand Down
Loading