Manual Reference Source Test
import InputElement from 'elementify/lib/elements/inputelement.js'
public class | version 1.0.0 | since 1.0.0 | source

InputElement

Extends:

elements/htmlelement~HtmlElement → InputElement

InputElement is the common basic class wrapping all vanilla HTML inputs. It provides all properties and method to :

  • get/set Element value
  • reset Element value
  • validate Element value

Test:

Constructor Summary

Private Constructor
private

InputElement constructor Node and options are passed through Element call.

version 1.0.0 since 1.0.0

Member Summary

Public Members
public get

Dirty state

version 1.0.0 since 1.0.0
public get

Fetch validation errors

version 1.0.0 since 1.0.0
public get

Get a unique name

  • nameattribute value
  • id attribute value if name not set
  • A random unique id if both name and value are not set
version 1.0.0 since 1.0.0
public get

Export rules set on the Element

version 1.0.0 since 1.0.0
public set

Import rules set on the Element

version 1.0.0 since 1.0.0
public get

Get standardized lowercased type attribute

version 1.0.0 since 1.0.0
public set

Set input type attribute

version 1.0.0 since 1.0.0
public get

Get value

version 1.0.0 since 1.0.0
public set

Set value

version 1.0.0 since 1.0.0
Private Members
private

Errors after validation

since 1.0.0
private

Rules for validation

since 1.0.0

Method Summary

Public Methods
public

Returns a label HtmlElement linked to this inputElement

version 1.0.0 since 1.0.0
public

rule(name: string, pattern: string | RegExp | CSSSelector | InputRuleCallback, message: string, evaluate: boolean): this

Add a validation rule.

version 1.0.0 since 1.0.0
public

Returns a tooltip HtmlElement linked to this inputElement

version 1.0.0 since 1.0.0
public

val(v: number | string): this | boolean | string | undefined

Get/Set value.

version 1.0.0 since 1.0.0
public

Validate the field against the rules.

version 1.0.0 since 1.0.0

Private Constructors

private constructor(node: Element, options: KeyValueObject): InputElement version 1.0.0 since 1.0.0 source

InputElement constructor Node and options are passed through Element call.

If the Element have required or pattern atributes, validation rules will be automatically set accordingly.

Params:

NameTypeAttributeDescription
node Element

Input element

options KeyValueObject

Options for the InputElement

Specific keys are :

  • value: initial value of the Element
  • options: list of options {value: text} for a select tag

Return:

InputElement

Test:

See:

Public Members

public get dirty: boolean version 1.0.0 since 1.0.0 source

Dirty state

Will be true if field value/state have changed false otherwise

See:

public get errors: Object version 1.0.0 since 1.0.0 source

Fetch validation errors

Test:

public get name: string version 1.0.0 since 1.0.0 source

Get a unique name

  • nameattribute value
  • id attribute value if name not set
  • A random unique id if both name and value are not set

public get rules: InputValidationRules version 1.0.0 since 1.0.0 source

Export rules set on the Element

Test:

public set rules: InputValidationRules version 1.0.0 since 1.0.0 source

Import rules set on the Element

public get type: string version 1.0.0 since 1.0.0 source

Get standardized lowercased type attribute

Test:

public set type: string version 1.0.0 since 1.0.0 source

Set input type attribute

public get value: boolean | string | undefined version 1.0.0 since 1.0.0 source

Get value

It normalizes the behaviour for boolean inputs (radio & checkbox) :

  • If no value attribute is set, it will return true or false
  • If a value attribute is set, it will return undefined and will be skipped in fields list if not checked or returns its value otherwise.

Test:

TODO:

  • - Add more casting (all values are considered as string when extracted)

public set value version 1.0.0 since 1.0.0 source

Set value

Private Members

private _errors: ObjectArray since 1.0.0 source

Errors after validation

private _rules: ObjectArray since 1.0.0 source

Rules for validation

Public Methods

public label(t: string, o: KeyValueObject): HtmlElement version 1.0.0 since 1.0.0 source

Returns a label HtmlElement linked to this inputElement

Params:

NameTypeAttributeDescription
t string

Text for label

o KeyValueObject

Options for HtmLElement

Return:

HtmlElement

Label

Test:

public rule(name: string, pattern: string | RegExp | CSSSelector | InputRuleCallback, message: string, evaluate: boolean): this version 1.0.0 since 1.0.0 source

Add a validation rule. Some presets are available :

  • required : Value must be not empty. Add a required attribute to the Element
  • sameas : Value must be the same than another field. You must provide a CSS Selector to query this field as second argument
  • int : Strict integer (only numbers)
  • number : Decimal number (with dot). Change Element type attribute to number
  • email : E-mail pattern. Change Element type attribute to email

    For custom error messages when validating through browser or programmatically, you can set the title attribute of the InputElement or provide a message when creating rule.

Params:

NameTypeAttributeDescription
name string

Name of the rule (preset or custom)

pattern string | RegExp | CSSSelector | InputRuleCallback
  • optional

Presets will usually don't require this value

  • For preset sameas, you must provide the required CSS selector to find the other Element in order to check value.
  • For custom rule, you can provide a valid RegExp object or a callback. Any other value will be evaluated as a RegExp if fourth parameter is set to true.
message string
  • optional

A message linked to a non validated rule can be store here

evaluate boolean
  • optional
  • default: false

If true, the method will try to evaluate patternparameter as a regular expression. If false, it will simply don't create rule if pattern is not a Function or a RegExp.

Return:

this

Chainable

Test:

TODO:

  • Add more presets

public tooltip(t: $type, o: KeyValueObject): HtmlElement version 1.0.0 since 1.0.0 source

Returns a tooltip HtmlElement linked to this inputElement

Params:

NameTypeAttributeDescription
t $type

Text for tooltip

o KeyValueObject

Options for HtmLElement

Return:

HtmlElement

Tooltip

Test:

public val(v: number | string): this | boolean | string | undefined version 1.0.0 since 1.0.0 source

Get/Set value. It's a chainable alias for the getter/setter properties

Params:

NameTypeAttributeDescription
v number | string
  • optional

Value to set

Return:

this | boolean | string | undefined

Test:

See:

public validate(): boolean version 1.0.0 since 1.0.0 source

Validate the field against the rules. Any errors will be logged in errors property.

Return:

boolean

trueif field validates, false otherwise.

Test: