Security News
Weekly Downloads Now Available in npm Package Search Results
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.
forms-generator
Advanced tools
Forms Generator is a library for Node.js that helps with HTML forms and lists (menus), including simple definitions, translation, data transmission and validation.
The main interface consists of Form
and Menu
classes, for forms
and menus definitions respectively. These definition classes could be
directly rendered to HTML via render
method. Also getContent
method could be used to get object suitable as an argument to Jade
Form
and Menu
mixins, so HTML will be rendered as a part of Jade
template.
Note: Neither i18n
nor jade
are not included in the
production dependencies, but rather they are expected by some methods
as arguments. Jade
should be compatible with version 1.8.0
and
i18n
with version 0.5.0
.
Each form field and menu item should have an id that is used for
several purposes. All ids should match /^[a-zA-Z_][a-zA-Z0-9_]*$/
regular expression.
The first one is generating HTML id attributes. All fields/items ids
are prefixed with the form/menu id. Also form field items ids are
prefixed with the field id. So generated HTML id attributes will look
like FormID-FieldID-EntryID
or MenuID-ItemID
. Single -
is
used as a nesting separator, --
is used to separate id suffixes for
additional elements like labels or wrappers.
The second one is generating translated labels for fields/items. By
default translation ids generation algorithm is the same as the HTML
one, but using non-prefixed ids is allowed. The first way is to
globally disable prefixes for an entire form/menu with noPrefix
option. The second one is to use nTP
function to disable prefixing
for just single id. HTML id attributes are not affected by these
options.
The last one is form data format. Forms field will have the same names
as ids. Also radio, select and checkbox field values will contain
values matching respective ids. Look at getExpectedValues
and
hasField
methods.
By default no any validation is performed. User supplied asynchronous
functions can be used for validation, set by setValidator
and
setGlobalValidator
methods. Each field can have one validation
function, that is validating only field data. Global validator can be
used for more complex validation, that has access to all fields
data. All data should be in Multiparty parser(which is provided by
FormParser
class) format. It is possible to run full validation with
validate
method, or execute just one validator with runValidatator
and runGlobalValidatator
methods.
array
) = id , type , [ attributes , ( entries | fields ) ]/^[a-zA-Z_][a-zA-Z0-9_]*$/
"div"
| "fieldset"
| "textarea"
| "select"
|
"datalist"
| "text"
| "password"
| "radio"
| "checkbox"
|
"file"
| "hidden"
| "button"
| "image"
| "reset"
|
"submit"
| "color"
| "date"
| "datetime"
|
"datetime-local"
| "email"
| "month"
| "number"
| "range"
|
"search"
| "tel"
| "time"
| "url"
| "week"
object
object
array
) = id , attributes , entriesEntries are allowed for "checkbox"
, "radio"
, "select"
and
"datalist"
types. Entries nesting is only allowed for "select"
,
the depth must be only of one level, so it makes possible to define
HTML select optgroups. "radio"
and "select"
select fields must
contain one or more entries.
Fields nesting is only allowed for "div"
and "fieldset"
types,
nested field are wrapped with the respective tag. "fieldset"
must
contain one or more fields, but "div"
can be empty and have a null
id.
Attributes objects allow to set input html elements
attributes. Note: style and class attributes are applied to a
field wrapper div element, so both input and label can be
styled. "radio"
and "checkbox"
entries styles are applied to a div
wrapper too.
array
) = id , url , [ attributes , items ]/^[a-zA-Z_][a-zA-Z0-9_]*$/
string
object
It is possible to insert html into generated forms and menus. The
special js object must be used. Key are the following selectors
prefixed by the element HTML id: ::before
and ::after
, for
insertion before and after an element respectively.
Values can be either HTML strings or arrays with mixin name and arguments. Note: Arguments are passed to a mixin as a single array argument. Also mixins should be defined on a global scope.
A complete Express 4 application is in example
directory.
Constructor
Throws:
Error
with a string
description on malformed items definitions.Arguments:
id
- string
matching /^[a-zA-Z_][a-zA-Z0-9_]*$/
regular
expression, or result of nTP
function.options
- object
with form options or null
. Fields:
noPrefix
- boolean
option to turn off prefixes for translation
ids, false
by default.attributes
- object
for form tag attributes or null
....fields
- Rest arguments are interpreted as field definitions.Method
Express form receive route helper.
Arguments:
router
mutable - Express router.callback
- Express route callback.Method [async]
Asynchronous form validation against previously defined validators via
setValidator
function. Order of fields and files validation
functions execution is undefined. After passing all local validations,
global validator set by setGlobalValidator
is executed. Local,
global or both validators can be undefined, meaning that any data pass
validation stage.
Arguments:
fields
- object
with Multiparty fields data or null
.files
- object
with Multiparty files data or null
.i18n
- i18n
translation library.callbackPass
- Function
called on successful form
validation. Arguments:
fields
- object
with Multiparty fields data.files
- object
with Multiparty files data.callbackFail
- function
called when form validation
fails. Arguments:
fieldErrors
- object
with field validation errors or null
.formError
- Result of global validator or null
.Method [async]
Run only one field validator.
Arguments:
fieldID
- string
with a field id.data
- object
with Multiparty field data.i18n
- i18n
translation library.
callback
- function
callback to run after validation. Arguments:
error
- true value
with an error or false
.data
- object
with Multiparty field data or null
.Method [async]
Execute only one field validator.
Arguments:
fields
- Multiparty fields data or null
.files
- Multiparty fields data or null
.i18n
- i18n
translation library.callback
- function
callback to run after validation.
error
- true value
with an error or false
.fields
- Multiparty fields data or null
.files
- Multiparty fields data or null
.Method
Validation helper. Gets expected values for radio, select and checkbox fields.
Arguments:
fieldID
- string
with a field id.Returns:
Array
with expected string
values, or an empty Array
if no
values are expected, or undefined
if a form has no such field.Method
Check whether or not a form has a field with the supplied id.
Arguments:
fieldID
- string
with a field id.Returns:
boolean
true
if a form has a field with the id, false
otherwise.Method [mutable]
Field validator setter. Validator should always call a cb and expect
field
to be null
or undefined
.
Arguments:
fieldID
- string
with a field id.validator
- function
validator. Arguments:
data
- Array
of Multiparty file/field data.i18n
- i18n
translation library.cb
- function
callback to run after validation. Arguments:
error
- true value
with an error or false
.data
- Array
of Multiparty file/field data.Method [mutable]
Global validator setter. Validator should always call a cb and expect
fields
, files
or both to be null
.
Arguments:
globalValidator
- function
global validator. Arguments:
fields
- Multiparty fields data or null
.files
- Multiparty fields data or null
.i18n
- i18n
translation library.cb
- function
callback to run after validation. Arguments:
error
- true value
with an error or false
.fields
- Multiparty fields data or null
.files
- Multiparty fields data or null
.Method [caches results]
Expands form for i18n
locale and caches results.
Arguments:
i18n
- i18n
translation library.Returns:
object
for Jade form render.Method
Renders HTML form.
Arguments:
jade
- jade
library.jadeMixinsPath
- string
with Jade mixins file.i18n
- i18n
translation library.insertionsObject
- object
with insertions data.Returns:
string
HTML form.Constructor
The same as multiparty.Form
. External form parser with the same
results format could be used.
Constructor
Throws:
Error
with a string
description on malformed items definitions.Arguments:
id
- string
matching /^[a-zA-Z_][a-zA-Z0-9_]*$/
regular
expression, or result of nTP
function.options
- object
with menu options or null
. Fields:
noPrefix
- boolean
option to turn off prefixes for translation
ids, false
by default.attributes
- object
for ul tag attributes or null
....items
- Rest arguments are interpreted as items definitions.Method [caches results]
Expands menu for i18n
locale and caches results.
Arguments:
i18n
- i18n
translation library.Returns:
object
for Jade menu render.Method
Renders HTML menu.
Arguments:
jade
- jade
library.jadeMixinsPath
- string
with Jade mixins file.i18n
- i18n
translation library.insertionsObject
- object
with insertions data.Returns:
string
HTML menu.Function
Wrapper for menu/form strings translation via __
function.
Arguments:
str
- string
to translate.Returns:
object
that will be translated with form/menu.
Function
Wrapper for menu/form strings translation via __n
function.
Arguments:
str
- string
to translate.Returns:
object
that will be translated with form/menu.
Function
Forces usage of unprefixed ids for translation.
Arguments:
str
- string
id.Returns:
object
that could be used as id in form/menu definitions.
Constant
Path to Jade mixins directory.
FAQs
A library that helps with HTML forms
The npm package forms-generator receives a total of 3 weekly downloads. As such, forms-generator popularity was classified as not popular.
We found that forms-generator demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.
Security News
A Stanford study reveals 9.5% of engineers contribute almost nothing, costing tech $90B annually, with remote work fueling the rise of "ghost engineers."
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.