The Forms Unit

flitter-forms/FormsUnit

exports:  class FormsUnit

class FormsUnit

extends:  libflitter/Unit

directory

type:  String

Stores the fully-qualified path to the directory that holds the validator definitions. Each of the files in this directory should export an Object of key-value pairs such that each of the keys is the name of an input field, and each of the values is an Array of validator check names.

constructor(String directory = './app/validators')

overrides:  super.constructor()         returns:  this

Initializes the class. Resolves the directory into the fully-qualified path of the folder that holds the validator definitions and stores it in this.directory.

go(express app, function next)

overrides:  super.go()         returns:  undefined

Initializes flitter-forms. Registers this.deploy() as the forms deployment script. Creates a new Object in _flit.forms. Then, iterates over files in this.directory recursively and imports the validator definitions. For each definition, it creates a new instance of flitter-forms/Validator using the definition schema. Each of these instances is assigned a name which, according to Flitter convention, is parsed from the file name and follows the pattern for sub-directory naming. For example, the Validator created from the file auth/Login.validator.js would be named auth:Login. Each of these instances is stored in _flit.forms.validators by name. Then, an access function is created, as well as a function to initialize a new form in the session. This establishes the Forms context and calls the next function in the stack under it.

provides()

overrides:  super.provides()         returns:  Object

Returns an Object describing the services, templates, and directories provided by this unit. They are as follows:

Type Key Value Notes
Service Name name "flitter-forms" The name of the service. This is added to the _flit.services array so other units can check for this unit.
Directory directories.validators this.directory The fully-qualified path of the validator definition files. This is added to the _flit.directories store so other units can access it.
Template templates.validator Object Template that creates new config file in this.directory.
deploy(function callback)

returns:  undefined

Deploys the default ./validators directory with a sample validator definition file. Once this has completed, it calls the callback function.

Forms Context

Guarantees that the form validator schemata have been loaded and are available in _flit.forms.validators. Also makes available other utility functions.

_flit.forms

type:  Object

Holds variables and functions provided by this unit.

_flit.forms.validators

type:  Object

Contains the instances of flitter-forms/Validator created from the imported validation schemata such that the keys are the names of the schemata. Following Flitter convention, the names are parsed from the files and follow the convention for sub-directory naming. That is, sub-directories can be specified with : characters. For example, auth:LoginForm would reference app/validators/auth/LoginForm.validator.js.

_flit.forms.initialize(flitter-forms/Validator validator, express.request request)

returns:  undefined

Initializes the corresponding form of the provided validator in request.session. This provides access to the error messages.

_flit.validator(String name)

returns:  flitter-forms/Validator

Returns the validator in _flit.forms.validators with the corresponding name. Validator names are parsed from the schema files and follow the Flitter convention for sub-directory naming. That is, sub-directories can be specified with : characters. For example, auth:LoginForm would reference app/validators/auth/LoginForm.validator.js.

No Comments
Back to top