Fork me on GitHub

reform by fitzgen

A Narwhal module for working with HTML forms, similar to Django's forms.

Install

First, install Narwhal.

Once you have Narwhal up and running, just use tusk:

tusk install reform

And you're ready to go!

Hello, Form!

var REFORM = require("reform");

// Define a form by describing the data, not almost arbitrary HTML.
var helloForm = REFORM.Form({
    name: REFORM.TextField()
});

// Get some unvalidated data from somewhere, maybe an HTTP POST.
helloForm.bindData(unvalidatedData);

if (helloForm.isValid()) {
    print("Hello, " + helloForm.cleanedData.name + "!");
}

Pretty easy, huh? But check this out: just about every method call is chainable (where it makes sense). We can rewrite that example if we wanted to be cool and do everything in as few keystrokes as possible:

var REFORM = require("reform");

if (REFORM.Form({ name: REFORM.TextField() })
          .bindData(unvalidatedData)
          .isValid())
{
    print("Hello, " + helloForm.cleanedData.name + "!");
}

For another generally lighthearted illustration of using reform, check out this example.

Pre-defined Fields

All form fields are require-able from Reform's top level namespace. For example, var TextField = require("reform").TextField.

TextField
A field that takes arbitrary text input. Renders as an input type="text"
TextAreaField
The same as a TextField, but renders to a textarea
EmailField
A field that validates that the input is a valid email. Renders as an input type="text"
PasswordField
A field that takes arbitrary text input, but hides it visually because it is an input type="password".
IntegerField
A text input whose validated data will be coerced to a number via parseInt (base 10), and throws a validation error if there are any non digit characters in the input.
Button
A simple submit button.

Pre-defined multiple choice fields

Multiple choice fields are also require-able from Reform's top level namespace and behave almost identically to other types of fields. The only difference is specifying the choices. To define the available choices, pass a list of [<value>, <label>] pairs to the form's initial options. For example:

var seasons = require("reform").DropdownField({
    choices: [
        ["su", "Summer"],
        ["f", "Fall"],
        ["w", "Winter"],
        ["sp", "Spring"]
    ]
})
RadioChoiceField
A multiple choice field where only one option may be selected. Renders to radio buttons.
DropdownField
A multiple choice field where only one option may be selected. Renders to a dropdown menu.
CheckMultipleField
A multiple choice field where more than one option can be selected. Renders as checkboxes.

Writing custom form fields

TODO

Customizing the rendered HTML of forms and fields

This is as easy as defining a template and binding it to field.template for whichever field you wish to customize. Forms don't have their own templates, they are represented in HTML as just the sum of their fields' rendered markup.

To get an idea of what the templates should look like and how to use them, just inspect the default templates in a REPL session.

$ js
Rhino 1.7 release 3 PRERELEASE 2009 12 12
js> reform = require("reform")
[object Object]
js> form = reform.Form({
  >     name: reform.TextField({
  >         helpText: "Please enter your name."
  >     })
  > })
[object reform.Form]
js> form.fields.name.template
<((tag))>
  <label for="id_((name))">((label))</label>
  ((helpText))
  ((error))
  <input type="((inputType))"
         name="((name))"
         id="id_((name))"
         value="((value))" />
</((tag))>
js> form.toHtml()
<li>
  <label for="id_name">Name</label>
  <p class='form-help-text'>Please enter your name.</p>

  <input type="text"
         name="name"
         id="id_name"
         value="" />
</li>

You can specify what type of tag wraps the form field by passing the tag name as an argument to form.toHtml.

js> form.toHtml("div")
<div>
  <label for="id_name">Name</label>
  <p class='form-help-text'>Please enter your name.</p>

  <input type="text"
         name="name"
         id="id_name"
         value="" />
</div>

Form properties

form.fields
An object containing the fields that make up this form. This is where the argument that you pass to the Form constructor gets stored.
form.isBound
A boolean reflecting whether or not this form has been bound to data yet. See form.bindData() for more information.
form.prefix
A string that will be concatenated to each fields' name when rendering to avoid namespace collisions when rendering multiple clones of the same field to one HTML page. Defaults to an empty string. See also form.clone().
form.errors
Only exists after you call form.isValid() and only if there are validation errors. This is an object where each property name corresponds to a field in form.fields and the property's value is a validation error message as a string.
form._name
String. Used for the toString representation of a form. Defaults to "Form".

Form methods

TODO

Field properties

TODO

Field methods

TODO

Authors

Nick Fitzgerald

MIT Licensed

Copyright (c) 2010 Nick Fitzgerald

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.