<commit_before># Note

Experimental refactoring of caolan's code. Nothing to see yet :)


# Forms

Constructing a good form by hand is a lot of work. Popular frameworks like
Ruby on Rails and Django contain code to make this process less painful.
This module is an attempt to provide the same sort of helpers for node.js.

    npm install forms

## Contribute

This code is still in its infancy, and I'd really appreciate any contributions,
bug reports or advice. Especially on the following key areas:

* __Creating sensible default rendering functions that generate flexible,
  accessible markup__. This is an early priority because without being
  confident that the standard markup won't change under their feet, developers
  will not be able to adopt the module for any sort of production use.
* __Exploring write-once validation that works on the client and the server__.
  There are some unique advantages to using the same language at both ends,
  let's try and make the most of it!
* __Ensuring it's easy to use with existing node web frameworks__. Ideally this
  module would integrate well with projects using any of the popular frameworks.
### Contributors

* [luddep](http://github.com/luddep)


## Example

    /**
     * Handles a GET/POST request for a form.
     */
    function handle_request(request, response, next) {
        forms = require('forms');
        var form = new forms.Form(example_form, 'item_example');
    }

    /**
     * Creates example form definition.
     *
     * @param {param}
     *   The parameter passed in as second argument to Form().
     * @param {callback}
     *   The function to pass the form the definition on to.
     */
    function example_form(param, callback) {

        // Load an item from the database. This can be asynchronous.
        item.load(id, function(err, item) {

            // Build the form definition and pass it on to the callback.
            callback(err, {
                /**
                 * Form method. Optional. Default: POST.
                 */
                method: 'GET',

                /**
                 * Form action. Optional. Default: ''.
                 */
                action: '/update',

                /**
                 * Returns an object describing a form. Mandatory.
                 */
                fields: function() {
                    var forms = require('forms');
                    var field_def = {};
                    // Generate form depending on loaded item.
                    for (var i in items.attributes) {
                        var field = items.attributes[i];
                        field_def[field.id] = forms.fields.string({
                            label: field.label,
                            value: item.data[field.id]
                        });
                    }
                    // Add a submit button.
                    field_def['submit'] = forms.fields.submit({
                        value: 'Submit',
                        submit: function(form, request, response, next) {
                            for (var i in item.attributes) {
                                if (form.data[i]) {
                                    item.data[i] = form.data[i];
                                }
                            }
                            item.save(function() {
                                response.redirect('/' + item.path)
                            });
                        }
                    });
                    return field_def;
                },

                /**
                 * Renders form. Mandatory.
                 */
                render: function(form, request, response, next) {
                    response.render(
                        'content', {
                            locals: item.renderForm(request, form)
                    });
                }
            });
        });
    }

## Available types

A list of the fields, widgets, validators and renderers available as part of
the forms module. Each of these components can be switched with customised
components following the same API.

### Fields

* string
* number
* boolean
* array
* password
* email
* url
* submit
* html

### Widgets
<commit_msg>Update readme.<commit_after># Note

Experimental refactoring of caolan's code. Nothing to see yet :)


# Forms

Constructing a good form by hand is a lot of work. Popular frameworks like
Ruby on Rails and Django contain code to make this process less painful.
This module is an attempt to provide the same sort of helpers for node.js.

    npm install forms

## Contribute

This code is still in its infancy, and I'd really appreciate any contributions,
bug reports or advice. Especially on the following key areas:

* __Creating sensible default rendering functions that generate flexible,
  accessible markup__. This is an early priority because without being
  confident that the standard markup won't change under their feet, developers
  will not be able to adopt the module for any sort of production use.
* __Exploring write-once validation that works on the client and the server__.
  There are some unique advantages to using the same language at both ends,
  let's try and make the most of it!
* __Ensuring it's easy to use with existing node web frameworks__. Ideally this
  module would integrate well with projects using any of the popular frameworks.
### Contributors

* [luddep](http://github.com/luddep)


## Example

    /**
     * Handles a GET/POST request for a form.
     */
    function handle_request(request, response, next) {
        forms = require('forms');
        var form = new forms.Form(function(callback){

            // Load an item from the database. This can be asynchronous.
            item.load(function(item) {

                // Build the form definition and pass it on to the callback.
                callback({
                    /**
                     * Form method. Optional. Default: POST.
                     */
                    method: 'GET',

                    /**
                     * Form action. Optional. Default: ''.
                     */
                    action: '/update',

                    /**
                     * Returns an object describing a form. Mandatory.
                     */
                    fields: function() {
                        var forms = require('forms');
                        var field_def = {};
                        // Generate form depending on loaded item.
                        for (var i in items.attributes) {
                            var field = items.attributes[i];
                            field_def[field.id] = forms.fields.string({
                                label: field.label,
                                value: item.data[field.id]
                            });
                        }
                        // Add a submit button.
                        field_def['submit'] = forms.fields.submit({
                            value: 'Submit',
                            submit: function(form, request, response, next) {
                                for (var i in item.attributes) {
                                    if (form.data[i]) {
                                        item.data[i] = form.data[i];
                                    }
                                }
                                item.save(function() {
                                    response.redirect('/' + item.path)
                                });
                            }
                        });
                        return field_def;
                    },

                    /**
                     * Renders form. Mandatory.
                     */
                    render: function(form, request, response, next) {
                        response.render(
                            'content', {
                                locals: item.renderForm(request, form)
                        });
                    }
                });
            });
        });
    }

## Available types

A list of the fields, widgets, validators and renderers available as part of
the forms module. Each of these components can be switched with customised
components following the same API.

### Fields

* string
* number
* boolean
* array
* password
* email
* url
* submit
* html

### Widgets

{'fraction_non_alphanumeric': 0.07551214864221058, 'fraction_numerical': 0.0, 'mean_word_length': 1.3164137931034483, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 9, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1285925', 'n_tokens_mistral': 1951, 'n_tokens_neox': 1815, 'n_words': 738}