Constructor
new Form(elementnullable)
Parameters:
| Name | Type | Attributes | Description |
|---|---|---|---|
element |
HTMLFormElement |
<nullable> |
Extends
Members
(static) trimValues :boolean
By default, leading and trailig spaces in string values are trimmed when obtained with this class. Set this property to false if you want to leave then untouched.
Type:
- boolean
(protected, constant, non-null) app :jrt.App
Reference to the app instance, just for convenience.
Type:
- Overrides:
(constant, non-null) element :HTMLFormElement
The HTML element represented by this instance.
Type:
- HTMLFormElement
- Overrides:
Methods
(static) getSubmitData(formnon-null, clickedButtonnullable, paramsnon-null)
Copies the current form control values of the specified form into the params
object. Only elements that have a name defined are included (the element's id is not
considered in this method).
If clickedButton is specified, its value is also returned (if the button
has a name). Any other button's values are not included.
Values are returned in the type specified by the element's data-type attribute,
which can be "string" or "number". For INPUT elements with
"number" type, the default type is "number". For all other elements,
the default type is "string".
When converting to number, both the "." and the "," are accepted as decimal point. An invalid number will be converted to NaN. All values are trimmed before conversion.
If an INPUT of type "checkbox" has a value attribute specified, its value will
only be included in the response if it's checked. Otherwise, the value included will
be the checkbox' checked state (a boolean).
If an INPUT of type "radio" has no value specified, its value when
checked will not be "on" (as would happen in a real form submission), but "" instead.
If no radio button in a group is checked, no value will be included in the response.
The selected values of a multiple-selection SELECT are returned as a string,
using the ';' character as separator (the "number" data type is not supported
in this case).
Parameters:
| Name | Type | Attributes | Description |
|---|---|---|---|
form |
HTMLFormElement | ||
clickedButton |
HTMLButtonElement |
<nullable> |
The submit button that was clicked, if available. |
params |
Object.<!jrt.FormValue> | Object to add the form params to. |
(static) getSubmitValue(elem) → (nullable) {jrt.FormValue}
Gets the specified form elements's submit value (regardless of the element's name/id). It returns null if the element's value is not eligible for submission.
The elements that can have their value submitted are SELECT, INPUT,
TEXTAREA and BUTTON. This method can also get the value
of a RadioNodeList.
Values are converted as explained in getSubmitData.
Parameters:
| Name | Type | Description |
|---|---|---|
elem |
jrt.FormControl | RadioNodeList |
Returns:
- Type
- jrt.FormValue
(static) getValue(elem) → {string|number}
Returns the specified element's value, if defined. The returned value can be a
string or number, depending on the element's data-type attribute.
Parameters:
| Name | Type | Description |
|---|---|---|
elem |
jrt.FormControl | HTMLOptionElement |
Returns:
- Type
- string | number
getButton(name) → (nullable) {HTMLButtonElement}
Gets a form's button element by name.
Parameters:
| Name | Type | Description |
|---|---|---|
name |
string |
Returns:
- Type
- HTMLButtonElement
getControl(name) → {jrt.FormControl|RadioNodeList}
Gets a form's control element by name.
Parameters:
| Name | Type | Description |
|---|---|---|
name |
string |
Returns:
- Type
- jrt.FormControl | RadioNodeList
getInput(name) → (nullable) {HTMLInputElement}
Gets a form's input element by name.
Parameters:
| Name | Type | Description |
|---|---|---|
name |
string |
Returns:
- Type
- HTMLInputElement
getOutput(name) → (nullable) {HTMLOutputElement}
Gets a form's output element by name.
Parameters:
| Name | Type | Description |
|---|---|---|
name |
string |
Returns:
- Type
- HTMLOutputElement
getRadioGroup(name) → {RadioNodeList|HTMLInputElement}
Gets a form's radio button group by name. If the radio group has only one element,
an instance of HTMLInputElement will be returned.
Parameters:
| Name | Type | Description |
|---|---|---|
name |
string |
Returns:
- Type
- RadioNodeList | HTMLInputElement
getSelect(name) → (nullable) {HTMLSelectElement}
Gets a form's select element by name.
Parameters:
| Name | Type | Description |
|---|---|---|
name |
string |
Returns:
- Type
- HTMLSelectElement
getTextArea(name) → (nullable) {HTMLTextAreaElement}
Gets a form's text area element by name.
Parameters:
| Name | Type | Description |
|---|---|---|
name |
string |
Returns:
- Type
- HTMLTextAreaElement
getValues() → (non-null) {Object.<!jrt.FormValue>}
Returns the current form values.
Returns:
- Type
- Object.<!jrt.FormValue>
(protected) handleEvent(enon-null, labelnullable)
This method is called when an event registered with listenToEvent
is triggered.
Parameters:
| Name | Type | Attributes | Description |
|---|---|---|---|
e |
Event | The event object. |
|
label |
string |
<nullable> |
The label specified when registering the event. |
- Overrides:
(protected) ignoreEvent(eventType, elementopt, nullable)
Stops listenig to the specified event type on the specified element.
Parameters:
| Name | Type | Attributes | Description |
|---|---|---|---|
eventType |
string | The event type to listen to. |
|
element |
Element |
<optional> <nullable> |
The HTML element to stop listening to. Defaults to this element. |
- Overrides:
(protected) listenToEvent(eventType, elementopt, nullable, labelopt, nullable, useCaptureopt)
Sets this object as the event listener for the specified event type and element.
The handleEvent method is called when the event occurs.
Parameters:
| Name | Type | Attributes | Description |
|---|---|---|---|
eventType |
string | The event type to listen to. |
|
element |
Element |
<optional> <nullable> |
The HTML element to listen to. Defaults to this element. |
label |
string |
<optional> <nullable> |
Optional label passed to the event handler. |
useCapture |
boolean |
<optional> |
- Overrides:
(protected) processResponse(reqnon-null, status, responsenullable)
Implement this method to process the response received after a call to sendRequest.
The response data type depends on the responseType property specified in the request.
The response status can be "success", "server-error", "type-error" (when the specified response type does not match the received data), "error" (if there is a connection error), "timeout" or "offline". Note that the "offline" status is not reliable in all devices, so an "error" status may be received when offline.
The default implementation does nothing.
Parameters:
| Name | Type | Attributes | Description |
|---|---|---|---|
req |
jrt.AjaxRequest | The request object. |
|
status |
string | The response status (see method description). |
|
response |
jrt.AjaxResponse |
<nullable> |
The server response. This will be null on error or timeout. |
- Overrides:
query(selector, baseElement) → (nullable) {HTMLElement}
Returns the first element that matches the specified selector. If no base element is specified, this block is used as base element. Returns null if no element is found.
Parameters:
| Name | Type | Description |
|---|---|---|
selector |
string | The CSS selector. |
baseElement |
HTMLElement | DocumentFragment | Optional base element. |
- Overrides:
Returns:
- Type
- HTMLElement
queryAll(selector, baseElement) → (non-null) {NodeList.<!HTMLElement>}
Returns all elements that match the specified selector. If no base element is specified, this element is used.
Parameters:
| Name | Type | Description |
|---|---|---|
selector |
string | The CSS selector. |
baseElement |
HTMLElement | DocumentFragment | Optional base element. |
- Overrides:
Returns:
- Type
- NodeList.<!HTMLElement>
(protected) sendRequest(reqnon-null)
Sends an asynchronous AJAX request to the URL specified in req'. The supported
methods are "GET" (the default), "POST", "PUT", "PATCH", "DELETE" and "HEAD".
The response will be passed to processResponse.
The expected response type can be specified with the responseType
property in the specified request ("json" by default).
Parameters:
| Name | Type | Description |
|---|---|---|
req |
jrt.AjaxRequest |
- Overrides:
Throws:
-
If any of the request parameters is invalid.
- Type
- Error
setValues(valuesnon-null)
Sets the current form values.
Parameters:
| Name | Type | Description |
|---|---|---|
values |
Object.<!jrt.FormValue> |