Skip to content

insin/newforms

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

js-forms

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).

Dependencies

  • DOMBuilder is required for interchangeable DOM Element / HTML generation.

    For Node.js, clone the DOMBuilder repository and add its path to your NODE_PATH environment variable, as the js-forms distribution module will attempt to require('DOMBuilder').

Basic Usage

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 js-forms 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 and an official distribution build - which will be forthcoming - here's a quick guide to getting started with js-forms.

  • Run build/build.py to generate a single js-forms.js distribution file, which assembles the component source files and defines a forms namespace object.

    The API exposed by this object is defined in build/formsnamespace.js.

    For Node.js, add the path to your checkout of js-forms to your NODE_PATH to be able to import it with require('js-forms'). If you install express and jade via npm (Node Package Manager), you can run node demo.js to see a basic example of js-forms in action.

  • Form constructors are created using the form.Forms factory function, which takes a single Object argument defining form fields and any other properties for the prorotype (validation methods etc.), returning a Form constructor which inherits from BaseForm:

    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 form.FormSet factory function, which takes a Form constructor and any additional properties for the FormSet defined as an Object, returning a FormSet constructor which inherits from BaseFormSet:

    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 used underscores_in_python become camelCasedInJavaScript.

    • 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 and errors become method calls; e.g. cleanedData() and errors().

      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 js-forms 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 their toString() methods make use of defaultRendering().

The unit tests exercise the library thoroughly, so dip in for examples of further usage in the meantime.