Class AsyncValidatedInput<T>
- Type Parameters:
T- The type of value being validated (typically String)
- All Implemented Interfaces:
ClientEventListener,InputListener,EventListener
AsyncValidatedInput extends ValidatedInput to support async validators that perform server-side operations like database lookups. Async validation is debounced on the client side to prevent excessive server calls during rapid typing.
Features:
- Combines sync and async validators
- Configurable debounce delay
- Race condition handling via request IDs
- Optional loading indicator
- Automatic UI updates on completion
Usage:
// Create input and error display
TextInput usernameInput = new TextInput();
usernameInput.setName("username");
Span usernameError = new Span();
Span usernameLoading = new Span().setText("Checking...");
// Create async validated input
AsyncValidatedInput<String> username = new AsyncValidatedInput<>(usernameInput, usernameError)
.required("Username is required")
.minLength(3, "Username must be at least 3 characters")
.pattern("^[a-zA-Z0-9_]+$", "Only letters, numbers, and underscores")
.addAsyncValidator(new UniqueValidator<>(
value -> userService.usernameExists(value),
"Username is already taken"
))
.setDebounce(500)
.setLoadingIndicator(usernameLoading)
.validateOn(ValidationTrigger.BLUR_THEN_CHANGE);
// Add to form
form.addElement(usernameInput);
form.addElement(usernameError);
form.addElement(usernameLoading);
How it works:
- When the input value changes, sync validators run immediately
- If sync validators pass, async validation is queued with debounce
- A loading indicator is shown (if configured)
- Async validators run in a background thread via OorianWorkerThread
- On completion, results are checked against current request ID
- If still current, UI is updated with results
- Since:
- 2025
- Version:
- 1.0
- Author:
- Marvin P. Warble Jr.
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionAsyncValidatedInput(InputElement<?> input) Creates an AsyncValidatedInput wrapping the specified input element.AsyncValidatedInput(InputElement<?> input, StyledElement<?> errorDisplay) Creates an AsyncValidatedInput wrapping the specified input element with an error display. -
Method Summary
Modifier and TypeMethodDescriptionaddAsyncValidator(AsyncValidator<T> validator) Adds an async validator to this input.Returns the list of async validators.intReturns the debounce delay.Returns the loading indicator element.booleanReturns whether async validation is currently pending.voidonEvent(InputChangeEvent event) Invoked when the value of an input element changes.setDebounce(int ms) Sets the debounce delay for async validation.setLoadingIndicator(StyledElement<?> indicator) Sets the loading indicator element.validate()Performs full validation including async validators.validate(ValidationContext context) Validates the current value with context.Methods inherited from class com.oorian.validation.ValidatedInput
addValidator, alphanumeric, alphanumeric, clearValidation, creditCard, creditCard, date, date, email, email, getErrorDisplay, getInput, getLastResult, getTrigger, getValidators, isValid, length, length, maxLength, maxLength, minLength, minLength, numeric, numeric, onEvent, pattern, pattern, phone, phone, phone, range, range, required, required, setDisplayText, showErrors, url, url, validate, validateOn
-
Constructor Details
-
AsyncValidatedInput
Creates an AsyncValidatedInput wrapping the specified input element.- Parameters:
input- The input element to validate
-
AsyncValidatedInput
Creates an AsyncValidatedInput 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
-
addAsyncValidator
Adds an async validator to this input.- Parameters:
validator- The async validator to add- Returns:
- This AsyncValidatedInput for method chaining
-
setDebounce
Sets the debounce delay for async validation.The debounce delay prevents async validation from being triggered too frequently during rapid typing. Default is 400ms.
- Parameters:
ms- The debounce delay in milliseconds- Returns:
- This AsyncValidatedInput for method chaining
-
setLoadingIndicator
Sets the loading indicator element.The loading indicator will have the 'visible' class added while async validation is in progress.
- Parameters:
indicator- The element to show/hide during async validation- Returns:
- This AsyncValidatedInput for method chaining
-
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- Overrides:
onEventin classValidatedInput<T>- Parameters:
event- the input change event containing the current value and optional state
-
validate
Performs full validation including async validators.This method blocks until async validation completes. For non-blocking validation triggered by user input, the event handler manages async flow.
- Overrides:
validatein classValidatedInput<T>- Returns:
- The combined validation result
-
validate
Description copied from class:ValidatedInputValidates the current value with context.- Overrides:
validatein classValidatedInput<T>- Parameters:
context- The validation context for cross-field validation- Returns:
- The validation result
-
isAsyncValidationPending
public boolean isAsyncValidationPending()Returns whether async validation is currently pending.- Returns:
- true if async validation is in progress
-
getAsyncValidators
Returns the list of async validators.- Returns:
- The list of async validators
-
getDebounceMs
public int getDebounceMs()Returns the debounce delay.- Returns:
- The debounce delay in milliseconds
-
getLoadingIndicator
Returns the loading indicator element.- Returns:
- The loading indicator as a StyledElement, or null if not set
-