A JavaScript port of Django's form-handling library, which runs in browsers (tested in the latest Firefox, Chrome and Opera browsers... and IE 6-8) and CommonJS environments (only tested with Node.js).
Node.js:
npm install newforms
Browser:
- newforms.min.js - 56KB (15KB gzipped)
- DOMBuilder is required for interchangeable DOM Element / HTML generation.
As direct porting from Django is nearing completion, the resulting API is very much focused on the server side. As such, Node.js is probably the best place to use newforms in anger at the moment, as you'll be working with a request/response cycle, which is Django forms' usual mode of operation.
Client side features, such as hooking into the DOM for instant validation and feedback, will be the focus of future work.
In lieu of documentation, head over to the Django forms documentation for a quick overview of the concepts behind this form library.
Here's a quick guide to getting started with newforms.
For Node.js, if you install express and jade via
npm
you can runnode demo.js
to see a basic example of newforms in action.All example code will assume you've imported like so:
var forms = require('newforms');
Form constructors are created using the
forms.Form
factory function, which takes a singleObject
argument defining form fields and any other properties for the prorotype (validation methods etc.), returning a Form constructor which inherits fromBaseForm
:var ContactForm = forms.Form({ subject: new forms.CharField({maxLength: 100}), message: new forms.CharField(), sender: new forms.EmailField(), ccMyself: new forms.BooleanField({required: false}) }); var form = new ContactForm();
FormSet constructors are created using the
forms.FormSet
factory function, which takes a Form constructor and any additional properties for the FormSet defined as anObject
, returning a FormSet constructor which inherits fromBaseFormSet
:var ArticleForm = forms.Form({ title: new forms.CharField(), pubDate: new forms.DateField() }); var ArticleFormSet = forms.FormSet(ArticleForm, {extra: 1}); var formSet = new ArticleFormSet();
The API is largely consistent with Django's API, with the following rules of thumb for converting between the two:
Where Django accepts keyword arguments, in Javascript a single
Object
argument is expected, with arguments expressed as its properties.Note that this applies anywhere Django accepts a keyword argument, even if the convention in Django is to pass certain keyword arguments positionally, e.g. when passing in POST data to a Form constructor.
Django (by convention):
f = MyForm(request.POST)
Javascript:
var f = new MyForm({data: req.body});
Method and variable names which use
underscores_in_python
becomecamelCasedInJavaScript
.Don't forget the
new
operator!Django:
forms.CharField(max_length=100)
JavaScript:
new forms.CharField({maxLength: 100})
Due to limited cross-browser support for properties in JavaScript, Form and FormSet properties such as
cleaned_data
anderrors
become method calls; e.g.cleanedData()
anderrors()
.It's ugly, but it works everywhere.
Objects which would be coerced to a string for display in Django, such as Forms, FormSets and ErrorLists, have a
defaultRendering()
method.This is required because newforms can output DOM Elements or HTML from the same objects and there's no standard
toDOM()
-type method in JavaScript. If you're operating in HTML mode, you can coerce these objects to string to get HTML out of them, as theirtoString()
methods make use ofdefaultRendering()
.
The unit tests exercise the library thoroughly, so dip in for examples of further usage in the meantime. Here are some pointers:
Forms:
- Custom validation per field and across fields
- Subclassing forms and faux-multiple inheritance/mixins
- Basic form processing in a view function
FormSets:
- Homage
- "newforms" was the old name for what is now django.forms when it was in development.
- Honesty
- You'll be typing "new forms" quite often if you use it.