Class ValidatedInput<T>
- Type Parameters:
T- The type of value being validated (typically String)
- All Implemented Interfaces:
ClientEventListener,InputListener,EventListener
- Direct Known Subclasses:
AsyncValidatedInput
ValidatedInput provides a fluent API for configuring validation rules on form inputs. It manages validators, listens for user interactions, and updates the UI to show validation feedback in real-time.
Features:
- Fluent API for adding validators
- Configurable validation triggers (blur, change, submit, etc.)
- Automatic UI updates for error display
- Integration with InputElement accessibility features
- Support for custom validators
Usage:
// Create input and error display elements
TextInput emailInput = new TextInput();
emailInput.setName("email");
Span emailError = new Span();
// Create validated input with fluent API
ValidatedInput<String> email = new ValidatedInput<>(emailInput, emailError)
.required("Email is required")
.email("Please enter a valid email address")
.validateOn(ValidationTrigger.BLUR_THEN_CHANGE);
// Add to form
form.addElement(emailInput);
form.addElement(emailError);
// Manual validation
if (email.validate().isValid()) {
// proceed
}
- Since:
- 2025
- Version:
- 1.0
- Author:
- Marvin P. Warble Jr.
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionValidatedInput(InputElement<?> input) Creates a ValidatedInput wrapping the specified input element.ValidatedInput(InputElement<?> input, StyledElement<?> errorDisplay) Creates a ValidatedInput wrapping the specified input element with an error display. -
Method Summary
Modifier and TypeMethodDescriptionaddValidator(Validator<T> validator) Adds a validator to this input.Adds an alphanumeric validator (letters and digits only).alphanumeric(String message) Adds an alphanumeric validator with a custom message.voidClears validation state and error display.Adds a credit card validator.creditCard(String message) Adds a credit card validator with a custom message.Adds a date validator with the specified format.Adds a date validator with format and custom message.email()Adds an email validator.Adds an email validator with a custom message.Returns the error display element.InputElement<?> getInput()Returns the underlying input element.Returns the result of the last validation.Returns the validation trigger.Returns the list of validators.booleanisValid()Returns whether the last validation was successful.length(int minLength, int maxLength) Adds a length range validator.Adds a length range validator with a custom message.maxLength(int maxLength) Adds a maximum length validator.Adds a maximum length validator with a custom message.minLength(int minLength) Adds a minimum length validator.Adds a minimum length validator with a custom message.numeric()Adds a numeric validator (digits only).Adds a numeric validator with a custom message.voidonEvent(InputChangeEvent event) Invoked when the value of an input element changes.voidonEvent(InputCompleteEvent event) Invoked when input to an element is completed.Adds a pattern validator.Adds a pattern validator with a custom message.phone()Adds a phone validator with US format (default).phone(PhoneValidator.Format format) Adds a phone validator with the specified format.phone(PhoneValidator.Format format, String message) Adds a phone validator with format and custom message.Adds a numeric range validator.Adds a numeric range validator with a custom message.required()Adds a required validator.Adds a required validator with a custom message.static voidsetDisplayText(StyledElement<?> display, String text) showErrors(boolean show) Sets whether to automatically show error messages.url()Adds a URL validator.Adds a URL validator with a custom message.validate()Validates the current value.validate(ValidationContext context) Validates the current value with context.final ValidationResultvalidate(ValidationContext context, Class<? extends ValidationGroup>... groups) Validates the current value with context and specific validation groups.validateOn(ValidationTrigger trigger) Sets when validation should be triggered.
-
Constructor Details
-
ValidatedInput
Creates a ValidatedInput wrapping the specified input element.- Parameters:
input- The input element to validate
-
ValidatedInput
Creates a ValidatedInput wrapping the specified input element with an error display.- Parameters:
input- The input element to validateerrorDisplay- The element to display error messages in (can be null)
-
-
Method Details
-
addValidator
Adds a validator to this input.- Parameters:
validator- The validator to add- Returns:
- This ValidatedInput for method chaining
-
required
Adds a required validator.- Returns:
- This ValidatedInput for method chaining
-
required
Adds a required validator with a custom message.- Parameters:
message- The error message- Returns:
- This ValidatedInput for method chaining
-
minLength
Adds a minimum length validator.- Parameters:
minLength- The minimum length- Returns:
- This ValidatedInput for method chaining
-
minLength
Adds a minimum length validator with a custom message.- Parameters:
minLength- The minimum lengthmessage- The error message- Returns:
- This ValidatedInput for method chaining
-
maxLength
Adds a maximum length validator.- Parameters:
maxLength- The maximum length- Returns:
- This ValidatedInput for method chaining
-
maxLength
Adds a maximum length validator with a custom message.- Parameters:
maxLength- The maximum lengthmessage- The error message- Returns:
- This ValidatedInput for method chaining
-
length
Adds a length range validator.- Parameters:
minLength- The minimum lengthmaxLength- The maximum length- Returns:
- This ValidatedInput for method chaining
-
length
Adds a length range validator with a custom message.- Parameters:
minLength- The minimum lengthmaxLength- The maximum lengthmessage- The error message- Returns:
- This ValidatedInput for method chaining
-
pattern
Adds a pattern validator.- Parameters:
pattern- The regex pattern- Returns:
- This ValidatedInput for method chaining
-
pattern
Adds a pattern validator with a custom message.- Parameters:
pattern- The regex patternmessage- The error message- Returns:
- This ValidatedInput for method chaining
-
email
Adds an email validator.- Returns:
- This ValidatedInput for method chaining
-
email
Adds an email validator with a custom message.- Parameters:
message- The error message- Returns:
- This ValidatedInput for method chaining
-
range
Adds a numeric range validator.- Parameters:
min- The minimum value (inclusive)max- The maximum value (inclusive)- Returns:
- This ValidatedInput for method chaining
-
range
Adds a numeric range validator with a custom message.- Parameters:
min- The minimum value (inclusive)max- The maximum value (inclusive)message- The error message- Returns:
- This ValidatedInput for method chaining
-
url
Adds a URL validator.- Returns:
- This ValidatedInput for method chaining
-
url
Adds a URL validator with a custom message.- Parameters:
message- The error message- Returns:
- This ValidatedInput for method chaining
-
phone
Adds a phone validator with US format (default).- Returns:
- This ValidatedInput for method chaining
-
phone
Adds a phone validator with the specified format.- Parameters:
format- The phone format (US, INTERNATIONAL, E164, ANY)- Returns:
- This ValidatedInput for method chaining
-
phone
Adds a phone validator with format and custom message.- Parameters:
format- The phone formatmessage- The error message- Returns:
- This ValidatedInput for method chaining
-
creditCard
Adds a credit card validator.- Returns:
- This ValidatedInput for method chaining
-
creditCard
Adds a credit card validator with a custom message.- Parameters:
message- The error message- Returns:
- This ValidatedInput for method chaining
-
date
Adds a date validator with the specified format.- Parameters:
format- The date format pattern (e.g., "yyyy-MM-dd", "MM/dd/yyyy")- Returns:
- This ValidatedInput for method chaining
-
date
Adds a date validator with format and custom message.- Parameters:
format- The date format patternmessage- The error message- Returns:
- This ValidatedInput for method chaining
-
numeric
Adds a numeric validator (digits only).- Returns:
- This ValidatedInput for method chaining
-
numeric
Adds a numeric validator with a custom message.- Parameters:
message- The error message- Returns:
- This ValidatedInput for method chaining
-
alphanumeric
Adds an alphanumeric validator (letters and digits only).- Returns:
- This ValidatedInput for method chaining
-
alphanumeric
Adds an alphanumeric validator with a custom message.- Parameters:
message- The error message- Returns:
- This ValidatedInput for method chaining
-
validateOn
Sets when validation should be triggered.- Parameters:
trigger- The validation trigger- Returns:
- This ValidatedInput for method chaining
-
showErrors
Sets whether to automatically show error messages.- Parameters:
show- true to show errors automatically- Returns:
- This ValidatedInput for method chaining
-
validate
Validates the current value.- Returns:
- The validation result
-
validate
Validates the current value with context.- Parameters:
context- The validation context for cross-field validation- Returns:
- The validation result
-
validate
@SafeVarargs public final ValidationResult validate(ValidationContext context, Class<? extends ValidationGroup>... groups) Validates the current value with context and specific validation groups.Only validators belonging to at least one of the specified groups will be run. If no groups are specified, all validators will run.
- Parameters:
context- The validation context for cross-field validationgroups- The validation groups to use (varargs)- Returns:
- The validation result
-
isValid
public boolean isValid()Returns whether the last validation was successful.- Returns:
- true if valid, false if invalid or never validated
-
getLastResult
Returns the result of the last validation.- Returns:
- The last validation result, or null if never validated
-
clearValidation
public void clearValidation()Clears validation state and error display. -
getInput
Returns the underlying input element.- Returns:
- The input element
-
getErrorDisplay
Returns the error display element.- Returns:
- The error display element, or null if not set
-
getValidators
Returns the list of validators.- Returns:
- The list of validators
-
getTrigger
Returns the validation trigger.- Returns:
- The validation trigger
-
onEvent
Description copied from interface:InputListenerInvoked when the value of an input element changes.This method is called continuously as the user types or modifies the input. Use this for real-time validation, character counting, live search, or other interactive feedback that should occur during data entry.
- Specified by:
onEventin interfaceInputListener- Parameters:
event- the input change event containing the current value and optional state
-
onEvent
Description copied from interface:InputListenerInvoked when input to an element is completed.This method is called when the user finishes editing the input, typically when the field loses focus or the user presses Enter. Use this for final validation, data persistence, or triggering actions based on the completed input value.
- Specified by:
onEventin interfaceInputListener- Parameters:
event- the input complete event containing the final value
-
setDisplayText
-