Members
config :object
Properties:
Name | Type | Description |
---|---|---|
action |
string | URL for form submission. |
attributes |
object | Key-value pairs for attributes in |
classes |
Array.<string> | List of CSS classes for |
formTemplate |
string | Mustache.js template for rendering HTML for |
errorsTemplate |
string | Mustache.js template for rendering
array of error messages for each field. It may be overridden in field
config. For simplicity, the classes are embedded in the template
instead of having an |
inputTemplates |
object | Key-value pairs where key is input
type (e.g. text, select) and value is Mustache.js template for
rendering HTML for input element. The appropriate input template is
used when rendering the input for a field and may be overridden in
field config. For dropdowns/checkboxes/radio buttons, the following
template variables are available: |
method |
string | HTTP method for form submission. |
name |
string | Form name. |
requiredText |
string | Text to return for error message if value is empty for required fields. Can be overridden in field config. |
Configuration defaults for form.
Type:
- object
fields :Map.<string, Fieldset>
Fields.
Key is fieldset name, value is Field
object.
Uses Map
instead of Object/Array
so that rendering order can be guaranteed by insertion
order and specific fields can be referenced easily by name instead of looping each time,
e.g. fields.get('myfield')
.
Name is made optional in the Field
object to reduce repetitions, e.g.
field.set('myfield', new Field({}))
instead of
field.set('myfield', new Field({ name:'myfield' }))
. It is also becos
the name of the field is dependent on the form that it is used in.
Type:
- Map.<string, Fieldset>
fieldsets :Map.<string, Fieldset>
Groups of fields, with each group rendered as a <fieldset>
element.
Key is fieldset name, value is Fieldset
object.
Uses Map
instead of Object/Array
so that rendering order can be guaranteed by insertion
order and specific fieldsets can be referenced easily by name instead of looping each time,
e.g. fieldsets.get('myfieldset')
.
Name is made optional in the Fieldset
object to reduce repetitions, e.g.
fieldsets.set('myset', new Fieldset({}))
instead of
fieldsets.set('myset', new Fieldset({ name:'myset' }))
. It is also becos
the name of the fieldset is dependent on the form that it is used in.
If at least one fieldset is specified, any fields not belonging to a fieldset will not be rendered.
Type:
- Map.<string, Fieldset>
Methods
(static) new Form(config) → {Form}
Constructor.
Parameters:
Name | Type | Description |
---|---|---|
config |
object | Form configuration. See |
Returns:
- Type
- Form
clearData() → {void}
Clear form data.
Each field will be set to its default value as per field.config.value
,
not an empty string, else buttons and html type fields will lose their
text.
Returns:
- Type
- void
getData() → {object}
Get form data.
Returns:
Key-value pairs where key is field name and value is field value. Type of value may be string, number or array (multiple values such as for multi-select checkbox group).
- Type
- object
render(templateVariablesopt) → {string}
Renders HTML for entire form.
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
templateVariables |
null | object |
<optional> |
null
|
Optional key-value pairs that can be used to add on to or override current template variables. |
Returns:
- Type
- string
setData(formData) → {void}
Set form data.
Parameters:
Name | Type | Description |
---|---|---|
formData |
object | Key-value pairs where key is field name and value is field value. Type of value may be string, number or array (multiple values such as for multi-select checkbox group). |
Returns:
- Type
- void
validate(formDataopt) → {null|object}
Validate form.
Values and errors, if any, will be stored in fields after validation. This is to aid when rendering the form after validation.
The method signature allows the validation of a form submission in one
line instead of having to call form.setData()
each time before calling
form.validate()
. Example scenario in an Express app:
// If need form.setData() then code for response will be duplicated
if ('GET' === request.method
|| ('POST' === request.method && !form.validate(request.body))
) {
response.send(mustache.render({ // Mustache.js
viewHtml, // template
{ record: record }, // template variables
{ form_html: form.render() } // partials
}));
}
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
formData |
null | object |
<optional> |
null
|
Optional key-value pairs where the key is the field name and the value is the submitted value. If null or unspecified, current values in fields will be used for validation. |
Returns:
Null returned if form is valid, else key-value pairs are returned, with key being field name and value being the error message for the field.
- Type
- null | object