Class AsyncValidatedInput<T>

java.lang.Object
com.oorian.validation.ValidatedInput<T>
com.oorian.validation.AsyncValidatedInput<T>
Type Parameters:
T - The type of value being validated (typically String)
All Implemented Interfaces:
ClientEventListener, InputListener, EventListener

public class AsyncValidatedInput<T> extends ValidatedInput<T>
ValidatedInput with support for asynchronous validation.

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:

  1. When the input value changes, sync validators run immediately
  2. If sync validators pass, async validation is queued with debounce
  3. A loading indicator is shown (if configured)
  4. Async validators run in a background thread via OorianWorkerThread
  5. On completion, results are checked against current request ID
  6. If still current, UI is updated with results
Since:
2025
Version:
1.0
Author:
Marvin P. Warble Jr.
See Also:
  • Constructor Details

    • AsyncValidatedInput

      public AsyncValidatedInput(InputElement<?> input)
      Creates an AsyncValidatedInput wrapping the specified input element.
      Parameters:
      input - The input element to validate
    • AsyncValidatedInput

      public AsyncValidatedInput(InputElement<?> input, StyledElement<?> errorDisplay)
      Creates an AsyncValidatedInput wrapping the specified input element with an error display.
      Parameters:
      input - The input element to validate
      errorDisplay - The element to display error messages in (can be null)
  • Method Details

    • addAsyncValidator

      public AsyncValidatedInput<T> addAsyncValidator(AsyncValidator<T> validator)
      Adds an async validator to this input.
      Parameters:
      validator - The async validator to add
      Returns:
      This AsyncValidatedInput for method chaining
    • setDebounce

      public AsyncValidatedInput<T> setDebounce(int ms)
      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

      public AsyncValidatedInput<T> setLoadingIndicator(StyledElement<?> indicator)
      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

      public void onEvent(InputChangeEvent event)
      Description copied from interface: InputListener
      Invoked 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:
      onEvent in interface InputListener
      Overrides:
      onEvent in class ValidatedInput<T>
      Parameters:
      event - the input change event containing the current value and optional state
    • validate

      public ValidationResult 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:
      validate in class ValidatedInput<T>
      Returns:
      The combined validation result
    • validate

      public ValidationResult validate(ValidationContext context)
      Description copied from class: ValidatedInput
      Validates the current value with context.
      Overrides:
      validate in class ValidatedInput<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

      public List<AsyncValidator<T>> 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

      public StyledElement<?> getLoadingIndicator()
      Returns the loading indicator element.
      Returns:
      The loading indicator as a StyledElement, or null if not set