# Class GvaScript.Form

Wrapper around HTML Forms

### Table of contents

NAME
SYNOPSIS
DESCRIPTION
Constructor
HTML
Markup of repeat elements
Autofocus
METHODS
destroy
getId
fire
register
unregister
init
to_hash
to_tree
fill_from_tree
add
remove
autofocus
GvaScript.Forms
METHODS
get
GvaScript.Form.Responders
METHODS
register
unregister
GvaScript.Form.EarlyResponders

## NAME

Class GvaScript.Form - Wrapper around HTML Forms

## SYNOPSIS

      <form id="my_form" onsubmit="return false;">
        <div repeat="foo">
          <h2>Foo #{foo.count}</h2>
          <input type="text" name="person.name"/>
          <input type="text" name="person.bdate"/>
          This is the repeated foo section
          <table>
            <tr repeat="bar">
              <td>Item #{bar.count}</td>
              <td><input name="#{bar.path}.buz"></td>
              <td><button onclick="GvaScript.Form.remove('#{bar.path}')">
                    Remove this row
                  </button></td>
            </tr>
          </table>
           <button onclick="GvaScript.Form.add('#{foo.path}.bar')">
              Add a bar
           </button>
        </div>
        <button onclick="GvaScript.Form.add('foo')">Add a foo</button>
      </form>

      <script>
        var gvascript_form = new GvaScript.Form('my_form', {
          datatree: {name:'kitty', bdate:'01.01.1990'},
          dataprefix: 'person',
          // 'active' form elements
          registry: [
            ['input', 'mouseover', function(event) {}],
            ['input', 'mouseout',  function(event) {}],
            ['input', 'change',    function(event, newv ,oldv) {}],
            ['input', 'init',      function(event, newv) {}]
          ],
          // handlers to form's custom events
          onInit: function(gva_form) {},
          onRepeatBlockRemove: function(gva_form, arg) {
            arg[0] // removed repeat name
            arg[1] // removed repeat path
          },
          onRepeatBlockAdd: function(gva_form, arg) {
            arg[0] // added repeat name
            arg[1] // added repeat path
          },
          onChange: function(gva_form, event) {
            // event.target -> element changed
            // event.memo.oldvalue -> changed element's old value
            // event.memo.newvalue -> changed element's new value
          },
          onSubmit: function(gva_form) {
            // ....
            // do form submission
            // ....
            if(success)               {gva_form.fire('AfterSubmit', arg)}
            else
            if(data_validation_error) {gva_form.fire('DataValidationError', arg)}
            else                      {gva_form.fire('SubmitFailure', arg)}
          },
          onAfterSubmit: function(gva_form, arg) {},
          onDataValidationError: function(gva_form, arg) {},
          onSubmitFailure: function(gva_form, arg) {},
          onBeforeDestroy: function(gva_form, arg) {}
        }
      </script>

## DESCRIPTION

This module of Alien::GvaScript manages forms with hierarchical fields and dynamically repeated sections. It works in close collaboration with Alien::GvaScript::Repeat.

The design is partially inspired by the `Web Forms 2.0` proposal (http://www.whatwg.org/specs/web-forms/current-work/), but is not an attempt to implement the proposed specification: there are some differences both in syntax and in semantics.

## Constructor

      new GvaScript.Form(form_id[, options])

- form_id

  id of the HTML form to extend

- options

  hash of optional properties that would define the behavior of the form

  - datatree

    tree represention of the initial form data.

    Ex:

          {
            name: 'barbie',
            age: '49',
            friends: [
              { name: 'ken', age: '35' },
              { name: 'jen', age: '55' }
            ]
          }

  - dataprefix

    sometimes it's helpful to prefix the html input names to give them 'context'.

    Ex: `doll.name` `doll.friends.0.name`.

    in this example, the dataprefix should be set to 'doll'

  - registry

    a list of arrays `[expression, eventName, handler]`.

    each item in the array would be passed over to the /notify method.

  - onInit

    function to be called after the successful initialization of the GvaScript.Form instance.

  - onRepeatBlockRemove

    function to be called after a block in a repeated section is removed

    Signature

          function onRepeatBlockRemove(gva_form, arg) {
            // gva_form : GvaScript.Form instance
            // arg: array containing repeat name as first element and repeat path as second
          }

  - onRepeatBlockAdd

    function to be called after a block in a repeated section is added

    Signature

          function onRepeatBlockAdd(gva_form, arg) {
            // gva_form : GvaScript.Form instance
            // arg: array containing repeat name as first element and repeat path as second
          }

  - onChange

    function to be called when any of the form's elements has changed or onAdd/onRemove of a repeatable section.

    Signature

          function onChange(gva_form, event) {
            // gva_form : GvaScript.Form instance
            // event: Event object fired by the change on the form element
            //        Has a memo object that has olvalue and newvalue set unless
            //        called from the Repeat module
          }

  - onBeforeSubmit

    function to be called directly before the form submits.

    Signature

          function onBeforeSubmit(gva_form) {
            // gva_form : GvaScript.Form instance
            // return false if you want to stop the form from submitting
          }

  - onSubmit

    function to be called when the form is submit.

    Signature

          function onSubmit(gva_form) {
            // gva_form : GvaScript.Form instance
            // use to fire 'onAfterSubmit', 'onSubmitFailure' or 'onDataValidationError'
          }

  - onBeforeDestroy

    function to be called right before the form instance calls the destroy method.

    Callback useful for doing some inhouse cleaning before the form gets removed from the DOM.

  - on[XYZ]

    Event handlers for custom form events. The form instance should fire these events so as corresponding handler can be fired.

  - actionsbar

    initializes and instance of Alien::GvaScript::ActionsBar.

    Signature

              actionsbar : {
                  container :  'container_id',
                  actions   : [
                      {
                          label : 'Submit Form',
                          type  : 'submit'
                      },
                      {
                          label : 'Init Form',
                          type  : 'button',
                          callback : function() {
                            // 'this' correspond to the button
                            GvaScript.Form.init('form', datatree);
                          }
                      }
                  ]
              }

## HTML

### Markup of repeat elements

Repeat elements may occur outside of forms and therefore are described in a separate document; see Alien::GvaScript::Repeat.

### Autofocus

The /init method inspects all form elements for an `autofocus` attribute; the first element that possesses this attribute automatically receives focus.

Similarly, when a repetition block is added into the form through the /add method, the first element within that repetition block that possesses an `autofocus` attribute automatically receives focus.

## METHODS

### destroy

      gvascript_form.destroy()

instance destructor - will unregister the GvaScript.Form instance from the GvaScript.Forms namespace and will remove all of its observers.

### getId

      gvascript_form.getId()

returns the id of the form element that the gvascript_form instance is wrapped around.

### fire

      gvascript_form.fire(eventName[, arg])

used to fire event on the for instance and thus called corresponding responders and instance callback.

`eventName`: eventName to fire without the 'on' prefix.

`arg` argument to send over to the corresponding responder(s) and callback.

this method will

1. execute the early responders set the on[eventName]

2. execute instance on[eventName] callback

3. execute the responders set the on[eventName]

execution sequence will stop whenever one of the responders or callback returns `false`

NOTE that the onBeforeSubmit, onSubmit events cannot be notified programatically. You actually need to call form.submit() for these events to be fired.

### register

       gvascript_form.register(expression, eventName, handler)
       GvaScript.Form.register(form, expression, eventName, handler)

tells the form to watch the eventName on elements that match the expression and to fire the corresponding handler.

`expression` (String) - A css selector

`eventName` (String) - The name of the event, in all lower case, without the "on" prefix — e.g., "click" (not "onclick").

A custom "init" event is also supported which is fired on an input that gets its value initialized with the initialization of the form.

`handler` (Function) - handler to fire.

Signature

      function handler(event[, newValue[, oldValue]]) {
        event.target // element being observed
      }

`newValue` is set for the `change` and the `init` events.

`oldValue` is set for the `change`.

NOTE: Form.Element.setValue method is wrapped in GvaSript?.ProtoExtensions module to programatically fire the blur event after setting the value on the input, and hence firing the change handler if any.

[This method can be called either as an instance method or as a generic method. If calling as a generic, pass the form HTMLElement/id in as the first argument.]

### unregister

       gvascript_form.unregister(expression[, eventName[, handler]])
       GvaScript.Form.unregister(form, expression[, eventName[, handler]])

removes observers set on given `expression`. if `eventName` is provided, will only stop observing that eventName. if `handler` is also provided, will just stop calling this handler.

NOTE that the same handler signature should be used in the register method for it to work in the unregister method.

[This method can be called either as an instance method or as a generic method. If calling as a generic, pass the form HTMLElement/id in as the first argument.]

### init

       gvascript_form.init(initial_tree, prefix)
       GvaScript.Form.init(form, initial_tree, prefix)

`form` is the id of a form (or directly the DOM element). Optional `initial_tree` is a javascript nested datastructure containing initial values for fields, that will be passed to the /fill_from_tree method.

[This method can be called either as an instance method or as a generic method. If calling as a generic, pass the form HTMLElement/id in as the first argument.]

### to_hash

      var flat_hash = GvaScript.Form.to_hash(form);

Inspects the contents of all fields in `form` and returns a flat hash of pairs (key-value).

[This method can be called either as an instance method or as a generic method. If calling as a generic, pass the form HTMLElement/id in as the first argument.]

### to_tree

      var tree = GvaScript.Form.to_tree(form);

Inspects the contents of all fields in `form` and returns a data tree, were dotted names in form names are expanded into sub-arrays or sub-hashes. So for example if the form looks like

      <input name="father.firstname"> <input name="father.lastname"><br>
      <input name="mother.firstname"> <input name="mother.lastname"><br>
      <div repeat="child" repeat-start="1">
        <input name="#{child.path}.firstname"><br>
      </div>

and if that form has been expanded with 3 repetition blocks for children, the resulting tree would be

      { "father" : {"firstname" : ..., "lastname": ...},
        "mother" : {"firstname" : ..., "lastname": ...},
        "child"  : [ {"firstname": ...},
                     {"firstname": ...},
                     {"firstname": ...} ] }

[This method can be called either as an instance method or as a generic method. If calling as a generic, pass the form HTMLElement/id in as the first argument.]

### fill_from_tree

      GvaScript.Form.fill_from_tree(form, field_prefix, tree);

Fills the form from values found in `tree` (this is the reverse of the /to_tree operation). Optional `field_prefix` is prepended to key names in `tree` for finding the corresponding form fields.

The method walks through nested subtrees in `tree`: the sequence of keys leading to a leaf is concatenated into a flat string, with dot separators, and if the form has a corresponding input element, the value of that element is set to the value of the leaf. Furthermore, if the initial tree contains repeated data sets (array subtrees), and if the form contains repeat elements with the same path, then new repetition blocks are dynamically created to match the number of items in the array: so if the form above is filled with the following tree

      { "child"  : [ {"firstname": "Abel"},
                     {"firstname": "Bob"},
                     {"firstname": "Cod"},
                     {"firstname": "Dave"}, ] }

then four repetition blocks will automatically created in the form.

[This method can be called either as an instance method or as a generic method. If calling as a generic, pass the form HTMLElement/id in as the first argument.]

### add

      GvaScript.Form.add(repeat_name, count);

Creates one or several new repetition blocks. The parameter `count` is optional and defaults to 1. Focus is automatically given to the first input element in the last repetition block that has an `autofocus` attribute (if any).

See Alien::GvaScript::Repeat/add for more explanations on the `add` operation.

### remove

      GvaScript.Form.remove(repeat_block[, live_update]);

Removes a repetition block from the DOM. The argument is either a DOM element or a string containing the element id.

param `Boolean` live_update: flag to indicate whether the 'remaining' repeatable sections are to be also removed from DOM, recreated by re-merging the data with repeat template, then finally re-appended to the DOM. Default true.

All repetition blocks above the removed block are renumbered, leaving no hole in the index sequence. To do so, these blocks are also removed from the DOM, and then added again through the /add method. This operation is implemented by Alien::GvaScript::Repeat/remove.

The recreated blocks are then re-populated with their previous input values.

### autofocus

      GvaScript.Form.autofocus(element);

Inspects DOM children of `element`, and gives focus to the first child that has an `autofocus` attribute.

## GvaScript.Forms

namespace that holds all active instances of GvaScript.Form

instances are unique per form id

### METHODS

#### get

      GvaScript.Forms.get(id)

finds and returns the GvaScript.Form instance where id is the unique id of the form HTMLElement.

## GvaScript.Form.Responders

A repository of global listeners notified about every step of the GvaSript.Form lifecycle.

Sometimes, you need to provide generic behaviors over all GvaScript.Form(s) in a single application.

To achieve this, GvaScript.Form provides global Responders that will be executed for every GvaScript.Form instance.

Responders by default are executed after the instance specific event callback.

### METHODS

#### register

      GvaScript.Form.Responders.register({eventName: handler}[, {eventName: handler}[, ...]]})

adding one or more global form events responders.

Supported eventNames: `onInit, onChange, onBeforeSubmit, onSubmit, on[XXX]` where `on[XXX]` is a custom event name.

#### unregister

      GvaScript.Form.Responders.unregister({eventName: handler}[, {eventName: handler}[, ...]]})

unregistering one or more global form events responders.

## GvaScript.Form.EarlyResponders

Identical to GvaScript.Form.Responders except that these responders would be executed before the instance specific callback.