First, install Narwhal.
Once you have Narwhal up and running, just use tusk
:
tusk install reform
And you're ready to go!
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.
All form fields are require-able from Reform's top level namespace. For
example, var TextField = require("reform").TextField
.
TextField
input
type="text"
TextAreaField
TextField
, but renders to
a textarea
EmailField
input type="text"
PasswordField
input type="password"
.IntegerField
parseInt
(base 10), and throws a validation error if there
are any non digit characters in the input.Button
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
DropdownField
CheckMultipleField
TODO
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.fields
Form
constructor gets
stored.
form.isBound
form.bindData()
for more information.
form.prefix
form.clone()
.
form.errors
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
toString
representation of a
form. Defaults to "Form".
TODO
TODO
TODO
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.