Developed and maintained by balestra
Licensed under Creative Commons Attribution 4.0 License for free personal/professional/commercial use, with attribution.
Includes some glyphicons, licensed to Marco Balestra.
Version 1.4, released on 2020-12-01
Some basic data types are provided with as-form-basic.js
:
as-form-basic.js
provides also a few basic themes:
light
)cyan
)black
)Plugin URI: https://cdn.altersoftware.org/js-as-form/plugin/as-form-basic.js
Plugin name: basic
Generates a single checkbox, returns true
if checked or nothing otherwise. Common option keys can be used.
In addition:
checked
(boolean), if true the field is initially checked.longswitch
(boolean), show long on/off switch instead of checkbox - defaults to true
.readonly
(boolean): if the field is read-only.Additional methods:
fakeField
.asField() - returns the asField
object.See also: truefalse
Notes:
value
, if any, is ignored: value will be true
.undefined
.mandatory
checkbox won’t validate if unchecked.Generates a buttons area (row), as a control field (not reported in form values, even if name
is still mandatory and must be unique in the form).
Common option keys can be used (if they make sense, e.g.: hidden
).
In addition:
list
(array), An array of button descriptions.A button description is a javascript object with the following keys:
btype
(string, optional): can be button
, submit
or reset
, defaults to button
.className
(string, optional): additional class name(s) (space separated) to be added to the button.icon
(string or false
, optional): the string (normally an SVG html) of the icon.submit
and reset
buttons icon is automatic, use icon:false
to have no icon.label
(string or false
, optional): the label of the button, defaults to capitalized button btype. Use label:false
to have no label.onclick
(function object, optional): use it in case of btype:'button'
to define the action of the button.E.g.:
myForm.addField([
'myButtons',
'buttons',
{
list:[
{label:'Exit',onclick:function(){ myForm.destroy(); } },
{btype:'reset'},
{btype:'submit'}
]
}
]);
Single checkbox. Common option keys can be used.
In addition:
checked
(boolean), if true the field is initially checked.longswitch
(boolean), show long on/off switch instead of checkbox.readonly
(boolean): if the field is read-only.Additional methods:
fakeField
.asField() - returns the asField
object.Notes:
undefined
.mandatory
checkbox won’t validate if unchecked, or if its value
is empty.An advanced field (or set of fields) where radio buttons and checkboxes can be used, also mixed and hierarchical.
Common option keys can be used, in addition:
ctype
(string, optional): Only the first character is cosidered. c
stands for checkbox
and r
for radio
. Defaults to c
(checkbox
).list
(array or object, mandatory, see below): the list of checkboxes/radio buttons.readonly
(boolean): if the field is read-only.list
as array
The array can contain strings or objects.
A single string Foo
becomes {label:"Foo",value:"Foo"}
, with name
and ctype
inherited by the parent.
An object can have the following keys:
checked
(boolean, optional): the initial state of the control, defaults to false
.ctype
(string, as above, optional): the type of control, defaults to parent ctype
.label
(string, mandatory): the label of the control.list
(array or object, optional, same rules): the list of children checkboxes/radio buttons.name
(string, optional): the target field name, default to parent name
.mandatory
(boolean, optional): the field with name
(declared or inherited) becomes mandatory.value
(string, optional): the value of the control, defaults to label
.list
as object
ctype
and name
are inherited by parent node.
It’s possible to override this behavior for a list of nodes by using list
as an object, with keys:
ctype
(string, as above, optional): the type of control, defaults to parent ctype
.name
(string, optional): the target field name, default to parent name
.list
(array, mandatory): the “list as array” described above.In this case ctype
and/or name
become the parent values for list
array of controls.
Notes:
name
(specified or inherited) are deselected.asForm
.getJS() method parses the value and returns the array.name
is mandatory
this applies also to all other controls under the same name
(declared or onherited).name
in list items can report multiple names comma separated. In this case the value of the controls when selected will span on all the named fields.mandatory
is true for multiple names it applies to every named field in the list.Common option keys can be used, but validation options are ignored (all but override
).
Notes:
getValue()
and setValue()
methods read and write HTML content.iscontrol
, nolabel
and nowarning
options are automatically set to true.value
options attribute, valued with a string or a DOM node. If a DOM node is used its innerHTML is copied, the node itself isn’t touched.Common option keys can be used. Dont’t forget that in case of validation failure no warning is shown.
Notes:
hidden
fields be careful with mandatory
, calc
, expr
etc.: if validation fails no warning is on display, but the form cannot submit anyway.Adds an horizontal ruler that looks fine on both mobile and desktop.
Option keys make no sense, and validation options are ignored (all but override
).
Notes:
<hr />
.getValue()
and setValue()
methods read and write HTML content.iscontrol
, nolabel
and nowarning
options are automatically set to true.Common option keys can be used.
In addition more optional keys:
max
(integer): maximum value.min
(integer): minimum value.onfocus
(function or string - function name), receives 2 parameters: field value, asField
object.onfocusout
(function or string - function name), receives 2 parameters: field value, asField
object.onkeyup
(function or string - function name), receives 2 parameters: field value, asField
object.placeholder
(string): Empty field placehoder.readonly
(boolean): if the field is read-only.Additional methods:
fakeField
.asField() - returns the asField
object.Common option keys can be used.
In addition:
options
(array of options or optgroups): select options, see below.size
(integer or 'auto'
): number of options (rows) to be displayed for select, defaults to 1e.g.:
f.addField(
'hair',
'select',
{
label: 'Hair',
options: [
{label:AS.label('selectChooseOne'),value:''},
{label:'None',value:'-'},
{label:'Dark:', options:['Black','Brown',{label:'Gray',value:'DarkGray'}]},
{label:'Light:',options:['Red','Blonde','Gray','White']},
'Other'
]
}
);
About options.options
options.options
is an array of options or optgroups{label: '…', value: … }
options.value
or options.default
{ label: '…', options: [ optionsOrOptgroups ] }
selectChooseOne
is pre-defined in the core labels loaded by AS.form
Additional methods:
fakeField
.asField() - returns the asField
object.This field is the key to define structured data: creates a new form that harvest details, and returns to the field a JSON representation of harvested data.
Recursion is allowed-
Common option keys can be used.
In addition more keys:
subform
(string or object, mandatory): the description of the subform. In case it’s a string can refer to a key of the subforms
option of the same field, or declared in asForm
options (useful if you want to share subforms definition among different fields).subforms
(object, optional): the description of the subforms used by the field, see below.subparts
(object, optional): elements of definition that different subforms can use and re-use - see below.Subform is a javascript object with 3 properties:
subtype
(string, mandatory): can be array
or object
, determines if the field can be repeated or not.preview
(array or function, optional): build the string representing a preview for structured data. The easiest option is to list in an array field names whose value (if not empty) will be the preview.values
(array, mandatory): a list of fields making the subform.subparts
({subpart:'subpartsKey'}
) or a common field property ({common:'commonOptionKey'}
).subpart
and common
additional attributes will overwrite the recalled object options.E.g:
myForm.addField(['name','subform',{ label:'Full Name', mandatory: true, subform: {
subtype: 'object',
preview: ['firstName','familyName'],
values: [
['firstName','text',{label:'First Name',normalize:true,mandatory:true}],
['middleName','text',{label:'First Name',normalize:true,skipempty:true}],
['lastName','text',{label:'First Name',normalize:true,mandatory:true}],
]
}}]);
When subform
is a string, its value must the the property of a subforms
object that could be declared both in the field options or in the form options.
E.g:
myForm.addField(['name','subform',{ label:'Full Name', mandatory: true, subform'fullName', subforms: {
fullName: {
subtype: 'object',
preview: ['firstName','familyName'],
values: [
['firstName','text',{label:'First Name',normalize:true,mandatory:true}],
['middleName','text',{label:'First Name',normalize:true,skipempty:true}],
['lastName','text',{label:'First Name',normalize:true,mandatory:true}],
]
}
}}]);
Both subform
and subforms
items can refer to shared objects defined in subparts
, and subparts
could be defined at field options level or at form option level (so that more fields can access it).
myForm.addField(['relatives','subform',{
subform:'relatives',
label:'Relatives',
subparts: {
title: ['title','select',{label:'Title',options:['Mr','Mrs']}],
firstName: ['firstName','text',{label:'First Name',normalize:true,mandatory:true,focus:true}],
middleName: ['middleName','text',{label:'Middle Name',normalize:true,skipempty:true}],
familyName: ['familyName','text',{label:'Family Name',normalize:true,mandatory:true}],
},
subforms : {
fullname: {
subtype: 'object',
preview: ['firstName','familyName'],
values: [
{subpart:'title'},
{subpart:'firstName'},
{subpart:'middleName'},
{subpart:'familyName'}
]
},
relatives: {
subtype: 'array',
preview: ['firstName','familyName'],
values: [
['name','subform',{subform:'fullname',label:'Full name',mandatory:true}],
['children','subform',{label:'Children',subform:'relatives',skipempty:true}]
]
},
}
}]);
This example illustrates also recursive use of subform.
Of course subparts
becomes more useful if it’s (and optionally subforms
is) defined at form options level.
An example is available here.
Common option keys can be used.
In addition more optional keys:
max
(integer): maximum string length.min
(integer): minimum string length.normalize
(boolean): if evaluated true
normalize text whitespaces (includes trim
).nospace
(boolean): if evaluated true
removes all whitespaces (includes trim
and normalize
).onfocus
(function or string - function name), receives 2 parameters: field value, asField
object.onfocusout
(function or string - function name), receives 2 parameters: field value, asField
object.onkeyup
(function or string - function name), receives 2 parameters: field value, asField
object.placeholder
(string): Fake field placehoder, when text field is empty.readonly
(boolean): if the field is read-only.skipempty
(boolean): if evaluated true
empty fields are disabled — not submitted, not reportd in asForm
.getJS()trim
(boolean): if evaluated true
trim leading and trailing text whitespaces.Additional methods:
fakeField
.asField() - returns the asField
object.Generates a single checkbox, returns true
if checked or false
otherwise. Common option keys can be used.
In addition:
checked
(boolean), if true the field is initially checked.longswitch
(boolean), show long on/off switch instead of checkbox - defaults to true
.readonly
(boolean): if the field is read-only.Additional methods:
fakeField
.asField() - returns the asField
object.See also: bool
Notes:
value
, if any, is ignored: value will be true
or false
.truefalse
checkboxes are part of fields sent with submit, valued false
.mandatory
checkbox won’t validate if unchecked.type
into subtype
type
into btype
formfunc
compilationformfunc
masksformfunc
field type for editor, to compile javascript functions