# Class: AbstractElement

## LuCI .ui. AbstractElement

The `AbstractElement` class serves as abstract base for the different widgets implemented by `LuCI.ui`. It provides the common logic for getting and setting values, for checking the validity state and for wiring up required events.

UI widget instances are usually not supposed to be created by view code directly, instead they're implicitely created by `LuCI.form` when instantiating CBI forms.

This class is automatically instantiated as part of `LuCI.ui`. To use it in views, use `'require ui'` and refer to `ui.AbstractElement`. To import it in external JavaScript, use `L.require("ui").then(...)` and access the `AbstractElement` property of the class instance value.

#### new LuCI.ui.AbstractElement()

ui.js, line 17

### Methods

#### getValue(){string|Array.<string>|null}

ui.js, line 81

  Read the current value of the input widget.

  ##### Returns:

  Type | Description
  --- | ---
  string | Array.<string> | null | The current value of the input element. For simple inputs like text fields or selects, the return value type will be a - possibly empty - string. Complex widgets such as `DynamicList` instances may result in an array of strings or `null` for unset values.

#### isChanged(){boolean}

ui.js, line 141

  Check whether the input value was altered by the user.

  ##### Returns:

  Type | Description
  --- | ---
  boolean | Returns `true` if the input value has been altered by the user or `false` if it is unchaged. Note that if the user modifies the initial value and changes it back to the original state, it is still reported as changed.

#### isValid(){boolean}

ui.js, line 154

  Check whether the current input value is valid.

  ##### Returns:

  Type | Description
  --- | ---
  boolean | Returns `true` if the current input value is valid or `false` if it does not meet the validation constraints.

#### registerEvents(targetNode, synevent, events)

ui.js, line 203

  Dispatch a custom (synthetic) event in response to received events.

  Sets up event handlers on the given target DOM node for the given event names that dispatch a custom event of the given type to the widget root DOM node.

  The primary purpose of this function is to set up a series of custom uniform standard events such as `widget-update`, `validation-success`, `validation-failure` etc. which are triggered by various different widget specific native DOM events.

  Name | Type | Description
  --- | --- | ---
  `targetNode` | Node | Specifies the DOM node on which the native event listeners should be registered.
  `synevent` | string | The name of the custom event to dispatch to the widget root DOM node.
  `events` | Array.<string> | The native DOM events for which event handlers should be registered.

#### render(){Node}

ui.js, line 290

  Render the widget, setup event listeners and return resulting markup.

  ##### Returns:

  Type | Description
  --- | ---
  Node | Returns a DOM Node or DocumentFragment containing the rendered widget markup.

#### setChangeEvents(targetNode, events)

ui.js, line 271

  Setup listeners for native DOM events that may change the widget value.

  Sets up event handlers on the given target DOM node for the given event names which may cause the input value to change completely, such as `change` events in a select menu. In contrast to update events, such change events will not trigger input value validation but they may cause field dependencies to get re-evaluated and will mark the input widget as dirty.

  Name | Type | Description
  --- | --- | ---
  `targetNode` | Node | Specifies the DOM node on which the event listeners should be registered.
  `events` | string | repeatable The DOM events for which event handlers should be registered.

#### setPlaceholder(value)

ui.js, line 113

  Set the current placeholder value of the input widget.

  Name | Type | Description
  --- | --- | ---
  `value` | string | Array.<string> | null | The placeholder to set for the input element. Only applicable to text inputs, not to radio buttons, selects or similar.

#### setUpdateEvents(targetNode, events)

ui.js, line 228

  Setup listeners for native DOM events that may update the widget value.

  Sets up event handlers on the given target DOM node for the given event names which may cause the input value to update, such as `keyup` or `onclick` events. In contrast to change events, such update events will trigger input value validation.

  Name | Type | Description
  --- | --- | ---
  `targetNode` | Node | Specifies the DOM node on which the event listeners should be registered.
  `events` | string | repeatable The DOM events for which event handlers should be registered.

#### setValue(value)

ui.js, line 99

  Set the current value of the input widget.

  Name | Type | Description
  --- | --- | ---
  `value` | string | Array.<string> | null | The value to set the input element to. For simple inputs like text fields or selects, the value should be a - possibly empty - string. Complex widgets such as `DynamicList` instances may accept string array or `null` values.

#### triggerValidation()

ui.js, line 168

  Force validation of the current input value.

  Usually input validation is automatically triggered by various DOM events bound to the input widget. In some cases it is required though to manually trigger validation runs, e.g. when programmatically altering values.

### Type Definitions

#### LuCI.ui.AbstractElement.InitOptionsObject

  ##### Properties:

  Name | Type | Argument | Default | Description
  --- | --- | --- | --- | ---
  `id` | string | <optional> |  | Specifies the widget ID to use. It will be used as HTML `id` attribute on the toplevel widget DOM node.
  `name` | string | <optional> |  | Specifies the widget name which is set as HTML `name` attribute on the corresponding `<input>` element.
  `optional` | boolean | <optional> | true | Specifies whether the input field allows empty values.
  `datatype` | string | <optional> | string | An expression describing the input data validation constraints. It defaults to `string` which will allow any value. See `LuCI.validation` for details on the expression format.
  `validator` | function | <optional> |  | Specifies a custom validator function which is invoked after the standard validation constraints are checked. The function should return `true` to accept the given input value. Any other return value type is converted to a string and treated as validation error message.
  `disabled` | boolean | <optional> | false | Specifies whether the widget should be rendered in disabled state (`true`) or not (`false`). Disabled widgets cannot be interacted with and are displayed in a slightly faded style.