Class ValidatedInput<T>

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

public class ValidatedInput<T> extends Object implements InputListener
Wrapper that adds validation capabilities to an InputElement.

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 Details

    • ValidatedInput

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

      public ValidatedInput(InputElement<?> input, StyledElement<?> errorDisplay)
      Creates a ValidatedInput 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

    • addValidator

      public ValidatedInput<T> addValidator(Validator<T> validator)
      Adds a validator to this input.
      Parameters:
      validator - The validator to add
      Returns:
      This ValidatedInput for method chaining
    • required

      public ValidatedInput<T> required()
      Adds a required validator.
      Returns:
      This ValidatedInput for method chaining
    • required

      public ValidatedInput<T> required(String message)
      Adds a required validator with a custom message.
      Parameters:
      message - The error message
      Returns:
      This ValidatedInput for method chaining
    • minLength

      public ValidatedInput<T> minLength(int minLength)
      Adds a minimum length validator.
      Parameters:
      minLength - The minimum length
      Returns:
      This ValidatedInput for method chaining
    • minLength

      public ValidatedInput<T> minLength(int minLength, String message)
      Adds a minimum length validator with a custom message.
      Parameters:
      minLength - The minimum length
      message - The error message
      Returns:
      This ValidatedInput for method chaining
    • maxLength

      public ValidatedInput<T> maxLength(int maxLength)
      Adds a maximum length validator.
      Parameters:
      maxLength - The maximum length
      Returns:
      This ValidatedInput for method chaining
    • maxLength

      public ValidatedInput<T> maxLength(int maxLength, String message)
      Adds a maximum length validator with a custom message.
      Parameters:
      maxLength - The maximum length
      message - The error message
      Returns:
      This ValidatedInput for method chaining
    • length

      public ValidatedInput<T> length(int minLength, int maxLength)
      Adds a length range validator.
      Parameters:
      minLength - The minimum length
      maxLength - The maximum length
      Returns:
      This ValidatedInput for method chaining
    • length

      public ValidatedInput<T> length(int minLength, int maxLength, String message)
      Adds a length range validator with a custom message.
      Parameters:
      minLength - The minimum length
      maxLength - The maximum length
      message - The error message
      Returns:
      This ValidatedInput for method chaining
    • pattern

      public ValidatedInput<T> pattern(String pattern)
      Adds a pattern validator.
      Parameters:
      pattern - The regex pattern
      Returns:
      This ValidatedInput for method chaining
    • pattern

      public ValidatedInput<T> pattern(String pattern, String message)
      Adds a pattern validator with a custom message.
      Parameters:
      pattern - The regex pattern
      message - The error message
      Returns:
      This ValidatedInput for method chaining
    • email

      public ValidatedInput<T> email()
      Adds an email validator.
      Returns:
      This ValidatedInput for method chaining
    • email

      public ValidatedInput<T> email(String message)
      Adds an email validator with a custom message.
      Parameters:
      message - The error message
      Returns:
      This ValidatedInput for method chaining
    • range

      public ValidatedInput<T> range(Number min, Number max)
      Adds a numeric range validator.
      Parameters:
      min - The minimum value (inclusive)
      max - The maximum value (inclusive)
      Returns:
      This ValidatedInput for method chaining
    • range

      public ValidatedInput<T> range(Number min, Number max, String message)
      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

      public ValidatedInput<T> url()
      Adds a URL validator.
      Returns:
      This ValidatedInput for method chaining
    • url

      public ValidatedInput<T> url(String message)
      Adds a URL validator with a custom message.
      Parameters:
      message - The error message
      Returns:
      This ValidatedInput for method chaining
    • phone

      public ValidatedInput<T> phone()
      Adds a phone validator with US format (default).
      Returns:
      This ValidatedInput for method chaining
    • phone

      public ValidatedInput<T> phone(PhoneValidator.Format format)
      Adds a phone validator with the specified format.
      Parameters:
      format - The phone format (US, INTERNATIONAL, E164, ANY)
      Returns:
      This ValidatedInput for method chaining
    • phone

      public ValidatedInput<T> phone(PhoneValidator.Format format, String message)
      Adds a phone validator with format and custom message.
      Parameters:
      format - The phone format
      message - The error message
      Returns:
      This ValidatedInput for method chaining
    • creditCard

      public ValidatedInput<T> creditCard()
      Adds a credit card validator.
      Returns:
      This ValidatedInput for method chaining
    • creditCard

      public ValidatedInput<T> creditCard(String message)
      Adds a credit card validator with a custom message.
      Parameters:
      message - The error message
      Returns:
      This ValidatedInput for method chaining
    • date

      public ValidatedInput<T> date(String format)
      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

      public ValidatedInput<T> date(String format, String message)
      Adds a date validator with format and custom message.
      Parameters:
      format - The date format pattern
      message - The error message
      Returns:
      This ValidatedInput for method chaining
    • numeric

      public ValidatedInput<T> numeric()
      Adds a numeric validator (digits only).
      Returns:
      This ValidatedInput for method chaining
    • numeric

      public ValidatedInput<T> numeric(String message)
      Adds a numeric validator with a custom message.
      Parameters:
      message - The error message
      Returns:
      This ValidatedInput for method chaining
    • alphanumeric

      public ValidatedInput<T> alphanumeric()
      Adds an alphanumeric validator (letters and digits only).
      Returns:
      This ValidatedInput for method chaining
    • alphanumeric

      public ValidatedInput<T> alphanumeric(String message)
      Adds an alphanumeric validator with a custom message.
      Parameters:
      message - The error message
      Returns:
      This ValidatedInput for method chaining
    • validateOn

      public ValidatedInput<T> validateOn(ValidationTrigger trigger)
      Sets when validation should be triggered.
      Parameters:
      trigger - The validation trigger
      Returns:
      This ValidatedInput for method chaining
    • showErrors

      public ValidatedInput<T> showErrors(boolean show)
      Sets whether to automatically show error messages.
      Parameters:
      show - true to show errors automatically
      Returns:
      This ValidatedInput for method chaining
    • validate

      public ValidationResult validate()
      Validates the current value.
      Returns:
      The validation result
    • validate

      public ValidationResult validate(ValidationContext context)
      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 validation
      groups - 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

      public ValidationResult 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

      public InputElement<?> getInput()
      Returns the underlying input element.
      Returns:
      The input element
    • getErrorDisplay

      public StyledElement<?> getErrorDisplay()
      Returns the error display element.
      Returns:
      The error display element, or null if not set
    • getValidators

      public List<Validator<T>> getValidators()
      Returns the list of validators.
      Returns:
      The list of validators
    • getTrigger

      public ValidationTrigger getTrigger()
      Returns the validation trigger.
      Returns:
      The validation trigger
    • 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
      Parameters:
      event - the input change event containing the current value and optional state
    • onEvent

      public void onEvent(InputCompleteEvent event)
      Description copied from interface: InputListener
      Invoked 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:
      onEvent in interface InputListener
      Parameters:
      event - the input complete event containing the final value
    • setDisplayText

      public static void setDisplayText(StyledElement<?> display, String text)