Class InputElement<T extends InputElement<T>>

Direct Known Subclasses:
Input, Select, TextArea

public abstract class InputElement<T extends InputElement<T>> extends StyledElement<T>
Base class for HTML input elements providing core input functionality.

This abstract base class extends StyledElement to provide common functionality for all input-like elements. It manages the input's name, value, and state, handles value change events, and provides HTML5 input attributes. This class serves as the foundation for the Input class and related input controls.

Features:

  • Name and value attribute management
  • Value change tracking and event dispatching
  • Initial value preservation for reset functionality
  • HTML5 input attributes (autocomplete, autofocus, placeholder, etc.)
  • Validation attributes (required, pattern, min, max, etc.)
  • Focus management with command support
  • Form association and validation control

Usage:

 // Typically extended by Input class
 public class CustomInput extends InputElement {
     public CustomInput() {
         super("input", true);
     }
 }
 
Since:
2007
Version:
1.0
Author:
Marvin P. Warble Jr.
See Also:
  • Constructor Details

    • InputElement

      public InputElement(String tagName)
      Creates an InputElement with the specified tag name as a self-closing element.
      Parameters:
      tagName - the HTML tag name (e.g., "input")
    • InputElement

      public InputElement(String tagName, boolean closed)
      Creates an InputElement with the specified tag name and closing behavior.
      Parameters:
      tagName - the HTML tag name
      closed - true for self-closing elements, false for elements with closing tags
  • Method Details

    • setName

      public final T setName(String name)
      Sets the name attribute for this input element.

      Specifies the name used as a key when submitting form data. The name identifies the input's value in the submitted form data.

      Parameters:
      name - The name of the input element.
      Returns:
      This element for method chaining.
    • setValue

      public final T setValue(String value)
      Sets the current value of the input element.
      Parameters:
      value - the value to set, or null to clear
    • setAccept

      public T setAccept(String accept)
      Sets the accept attribute specifying accepted content types for file inputs.

      Used on <input> elements with type="file" to filter file selection. Accepts comma-separated MIME types or file extensions (e.g., "image/*", ".pdf,.doc").

      Overrides:
      setAccept in class Element<T extends InputElement<T>>
      Parameters:
      accept - Comma-separated list of accepted content types.
      Returns:
      This element for method chaining.
    • setCapture

      public T setCapture(String capture)
      Sets the capture attribute for media capture on mobile devices.

      Used on <input> elements with type="file". Values include "user" (front-facing camera) and "environment" (rear-facing camera).

      Parameters:
      capture - The capture value (user, environment).
      Returns:
      This element for method chaining.
    • setChecked

      public T setChecked(boolean checked)
      Sets the checked boolean attribute.

      Indicates that the input should be pre-selected when the page loads. Used on <input> elements with type="checkbox" or type="radio".

      Parameters:
      checked - True to check, false to uncheck.
      Returns:
      This element for method chaining.
    • setAutocomplete

      public final T setAutocomplete(boolean on)
      Sets the autocomplete attribute to enable or disable browser autocomplete.
      Parameters:
      on - true to enable autocomplete, false to disable
      Returns:
      this element for method chaining
    • setDirName

      public final T setDirName(String dirname)
      Sets the dirname attribute for text directionality submission.

      Enables submission of the text directionality of the input field along with the form data. The value specifies the name under which the directionality will be submitted.

      Parameters:
      dirname - the name for text directionality submission
      Returns:
      this element for method chaining
    • setDisabled

      public final T setDisabled(boolean disabled)
      Sets the disabled boolean attribute.

      Indicates that the element should be disabled, preventing user interaction and form submission. Used on <button>, <fieldset>, <input>, <select>, and <textarea> elements. Disabled elements are typically rendered in a grayed-out style.

      Overrides:
      setDisabled in class VisualElement<T extends InputElement<T>>
      Parameters:
      disabled - True to disable, false to enable.
      Returns:
      This element for method chaining.
    • setForm

      public final T setForm(String formId)
      Associates this input with a form element.

      Specifies the form this input belongs to using the form's ID. This is useful when the input is placed outside of its associated <form> in the document.

      Parameters:
      formId - the ID of the form element to associate with
      Returns:
      This element for method chaining.
    • setFormNoValidate

      public final T setFormNoValidate(boolean flag)
      Sets the formnovalidate boolean attribute.

      Overrides the form's validation behavior for this specific submit button, bypassing constraint validation. Used on <input> elements with type="submit" or type="image".

      Parameters:
      flag - True to skip form validation, false to remove.
      Returns:
      This element for method chaining.
    • setFormAction

      protected T setFormAction(String formAction)
      Sets the formaction attribute.

      Overrides the form's action attribute for this specific submit button. Used on <input> elements with type="submit" or type="image". The value specifies the URL to submit the form data to.

      If the URL starts with "/" (absolute path within the application), the servlet context path will be automatically prepended.

      Parameters:
      formAction - The URL for form submission.
      Returns:
      This element for method chaining.
    • setFormEncType

      protected T setFormEncType(String formEncType)
      Sets the formenctype attribute.

      Overrides the form's enctype attribute for this specific submit button. Used on <input> elements with type="submit" or type="image".

      Parameters:
      formEncType - The encoding type for form submission.
      Returns:
      This element for method chaining.
    • setFormEncType

      protected T setFormEncType(FormEncoding formEncType)
      Sets the form encoding type override using the FormEncoding enum.
      Parameters:
      formEncType - The encoding type.
      Returns:
      This element for method chaining.
    • setFormMethod

      protected T setFormMethod(String formMethod)
      Sets the formmethod attribute.

      Overrides the form's method attribute for this specific submit button. Used on <input> elements with type="submit" or type="image".

      Parameters:
      formMethod - The HTTP method for form submission (get, post).
      Returns:
      This element for method chaining.
    • setFormMethod

      protected T setFormMethod(FormMethod formMethod)
      Sets the form method override using the FormMethod enum.
      Parameters:
      formMethod - The HTTP method (GET or POST).
      Returns:
      This element for method chaining.
    • setFormTarget

      protected T setFormTarget(String formTarget)
      Sets the formtarget attribute.

      Overrides the form's target attribute for this specific submit button, specifying where to display the response. Used on <input> elements with type="submit" or type="image". Values include "_self", "_blank", "_parent", and "_top".

      Parameters:
      formTarget - The browsing context for form submission.
      Returns:
      This element for method chaining.
    • setFormTarget

      protected T setFormTarget(Target formTarget)
      Sets the form target override using the Target enum.
      Parameters:
      formTarget - The browsing context for form submission.
      Returns:
      This element for method chaining.
    • setList

      public final T setList(String list)
      Sets the list attribute linking this input to a <datalist> element.

      The value must be the ID of a DataList element in the same document. The browser provides autocomplete suggestions from the datalist options.

      Parameters:
      list - the ID of the datalist element to link to
      Returns:
      this element for method chaining
    • setMax

      public final T setMax(String value)
      Sets the max attribute specifying the maximum allowed value.

      Used on input types like number, range, and date.

      Parameters:
      value - the maximum value
      Returns:
      this element for method chaining
    • setMaxLength

      public final T setMaxLength(String value)
      Sets the maximum character length allowed, parsing the string value as an integer.
      Parameters:
      value - the maximum number of characters as a string
      Returns:
      this element for method chaining
    • setMaxLength

      public final T setMaxLength(int value)
      Sets the maxlength attribute specifying the maximum number of characters allowed.
      Parameters:
      value - the maximum number of characters
      Returns:
      this element for method chaining
    • setMin

      public final T setMin(String value)
      Sets the min attribute specifying the minimum allowed value.

      Used on input types like number, range, and date.

      Parameters:
      value - the minimum value
      Returns:
      this element for method chaining
    • setMinLength

      public final T setMinLength(int value)
      Sets the minlength attribute specifying the minimum number of characters required.
      Parameters:
      value - the minimum number of characters
      Returns:
      this element for method chaining
    • setMultiple

      public final T setMultiple(boolean flag)
      Sets the multiple boolean attribute to allow selection of multiple values.
      Parameters:
      flag - true to allow multiple values, false to remove
      Returns:
      this element for method chaining
    • setPattern

      public final T setPattern(String regexp)
      Sets the regular expression pattern for input validation.

      The input's value must match this pattern for the form to be submitted. The pattern is matched against the entire value, not just a subset.

      Parameters:
      regexp - the regular expression pattern to validate against
      Returns:
      this element for method chaining
    • setPlaceholder

      public final T setPlaceholder(String placeholder)
      Sets the placeholder attribute providing a short hint for expected input.

      The placeholder text disappears when the user begins typing. Should not be used as a substitute for a <label> element.

      Parameters:
      placeholder - the placeholder text
      Returns:
      this element for method chaining
    • setReadOnly

      public final T setReadOnly(boolean flag)
      Sets the readonly boolean attribute preventing user modification.

      Unlike disabled, read-only fields remain focusable and their values are included in form submission.

      Parameters:
      flag - true to make read-only, false to remove
      Returns:
      this element for method chaining
    • setRequired

      public final T setRequired(boolean flag)
      Sets the required boolean attribute indicating the field must be filled before form submission.
      Parameters:
      flag - true to require input, false to remove
      Returns:
      this element for method chaining
    • setSize

      public final T setSize(int numChars)
      Sets the size attribute specifying the visible width in characters.
      Parameters:
      numChars - the number of visible characters
      Returns:
      this element for method chaining
    • setStep

      public final T setStep(String number)
      Sets the step interval for numeric and date/time inputs.

      Defines the legal number intervals for the input value. For example, "2" means only even numbers are valid. Use "any" to allow any value.

      Parameters:
      number - the step interval (e.g., "1", "0.01", "any")
      Returns:
      this element for method chaining
    • focus

      public final void focus()
      Requests focus on this input element.
    • getAutoComplete

      protected String getAutoComplete()
      Returns the autocomplete attribute value.
      Returns:
      the autocomplete value, or null if not set
    • getDirName

      protected String getDirName()
      Returns the dirname attribute value.
      Returns:
      the dirname value, or null if not set
    • getMax

      protected String getMax()
      Returns the max attribute value.
      Returns:
      the maximum value, or null if not set
    • getMaxLength

      protected int getMaxLength()
      Returns the maxlength attribute value.
      Returns:
      the maximum length, or -1 if not set
    • getMin

      protected String getMin()
      Returns the min attribute value.
      Returns:
      the minimum value, or null if not set
    • getMinLength

      protected int getMinLength()
      Returns the minlength attribute value.
      Returns:
      the minimum length, or -1 if not set
    • isMultiple

      protected boolean isMultiple()
      Returns whether the multiple attribute is set.
      Returns:
      true if multiple is present, false otherwise
    • getPlaceholder

      protected String getPlaceholder()
      Returns the placeholder attribute value.
      Returns:
      the placeholder text, or null if not set
    • isReadOnly

      protected boolean isReadOnly()
      Returns whether the readonly attribute is set.
      Returns:
      true if readonly is present, false otherwise
    • isRequired

      protected boolean isRequired()
      Returns whether the required attribute is set.
      Returns:
      true if required is present, false otherwise
    • getSize

      protected int getSize()
      Returns the size attribute value.
      Returns:
      the size, or -1 if not set
    • getFormAction

      protected String getFormAction()
      Returns the formaction attribute value.

      The formaction attribute overrides the action attribute of the parent form for this specific submit button. Used on <input type="submit"> and <input type="image"> elements.

      Returns:
      The form action URL, or null if not set.
    • getFormEncType

      protected String getFormEncType()
      Returns the formenctype attribute value.

      The formenctype attribute overrides the enctype attribute of the parent form for this specific submit button. Used on <input type="submit"> and <input type="image"> elements.

      Returns:
      The form encoding type, or null if not set.
    • getFormMethod

      protected String getFormMethod()
      Returns the formmethod attribute value.

      The formmethod attribute overrides the method attribute of the parent form for this specific submit button. Used on <input type="submit"> and <input type="image"> elements. Values are "get", "post", or "dialog".

      Returns:
      The form method, or null if not set.
    • isFormNoValidate

      protected boolean isFormNoValidate()
      Returns whether the formnovalidate attribute is set.

      The formnovalidate attribute overrides the novalidate attribute of the parent form, bypassing constraint validation for this specific submit button. Used on <input type="submit"> and <input type="image"> elements.

      Returns:
      true if formnovalidate is present, false otherwise.
    • getFormTarget

      protected String getFormTarget()
      Returns the formtarget attribute value.

      The formtarget attribute overrides the target attribute of the parent form for this specific submit button. Used on <input type="submit"> and <input type="image"> elements. Values include "_self", "_blank", "_parent", and "_top".

      Returns:
      The form target, or null if not set.
    • getName

      public final String getName()
      Returns the name attribute value.
      Returns:
      the input name, or null if not set
    • getValue

      public String getValue()
      Returns the current value of the input.
      Returns:
      the current value
    • onValueInitialized

      public void onValueInitialized(String value, String state)
      Called when the value is first initialized from the client.
      Parameters:
      value - the initial value from the client
      state - the initial state information
    • onValueUpdate

      public void onValueUpdate(String value, String state)
      Called when the value is updated from the client.
      Parameters:
      value - the new value from the client
      state - the current state information
    • onValueChange

      protected void onValueChange()
      Called when the input value changes. Override to handle value change events.
    • reset

      public boolean reset()
      Resets the input value to its initial value.
      Returns:
      true if the value was changed, false if already at initial value
    • associateLabel

      public final InputElement associateLabel(Label label)
      Associates this input with a label element.

      Creates a proper label-input binding for accessibility. Screen readers will announce the label text when the input receives focus.

      Parameters:
      label - The Label element to associate with this input.
      Returns:
      This element for method chaining.
    • setErrorDisplay

      public final InputElement setErrorDisplay(Element errorElement)
      Sets up an accessible error display for this input.

      Associates an element that will contain error messages for this input. When aria-invalid is set to true, screen readers will announce the error message.

      Usage:

      
       TextInput email = new TextInput();
       Span errorSpan = new Span();
       errorSpan.setId("email-error");
       email.setErrorDisplay(errorSpan);
      
       // When validation fails:
       email.setHasError(true);
       errorSpan.setText("Please enter a valid email address");
       
      Parameters:
      errorElement - The element that will contain error messages.
      Returns:
      This element for method chaining.
    • setHasError

      public final InputElement setHasError(boolean hasError)
      Marks this input as having or not having an error.

      Sets aria-invalid appropriately and should be used in conjunction with an error display element configured via setErrorDisplay(Element).

      Parameters:
      hasError - True if the input has a validation error.
      Returns:
      This element for method chaining.
    • setDescription

      public final InputElement setDescription(Element descriptionElement)
      Associates this input with an element providing additional description.

      The referenced element's text will be announced by screen readers after the input's label, providing additional context or instructions.

      Usage:

      
       TextInput password = new TextInput();
       Span hint = new Span("Password must be at least 8 characters");
       hint.setId("password-hint");
       password.setDescription(hint);
       
      Parameters:
      descriptionElement - The element containing the description text.
      Returns:
      This element for method chaining.
    • setRequiredAccessible

      public final InputElement setRequiredAccessible(boolean required)
      Sets this input as required with proper accessibility attributes.

      Sets both the HTML5 required attribute and aria-required for screen readers.

      Parameters:
      required - True if the input is required.
      Returns:
      This element for method chaining.