<?xml version="1.0" encoding="iso-8859-1"?>
<?xml-stylesheet type="text/xsl" href="/lib/xsl/devedge-1.00/article_en.xsl"?>
<nde:article 
  url="/toolbox/examples/2003/CFormData/" 
  xmlns="http://www.w3.org/1999/xhtml"
  xmlns:nde="http://devedge.netscape.com/2002/de" 
  xmlns:ent="http://devedge.netscape.com/2003/ent"
  xml:lang="en">

  <nde:header>

    <nde:title>
      Simplify Form handling With the CFormData JavaScript object.
    </nde:title>

    <nde:category>
      Example
    </nde:category>

    <nde:pubdate year="2003" month="06" day="06"/>
    <nde:moddate year="2003" month="07" day="14"/>

    <nde:summary>
      Dealing with Form data can be a tedious task. Check out how the 
      CFormData JavaScript object makes dealing with forms as easy 
      as pie.
    </nde:summary>
    
    <nde:channels>
      <nde:channel id="dom"/>
      <nde:channel id="examples"/>
    </nde:channels>
    
    <nde:keywords>
      Netscape, Netscape 7, Mozilla, Gecko, W3C, W3C DOM, HTML Events, Form, forms, Form Handling
    </nde:keywords>
    
    <nde:authlist>
      <nde:author>
        <nde:authname>Bob Clary</nde:authname>
        <nde:authtitle>Evangelist</nde:authtitle>
        <nde:authaffil>Netscape Communications</nde:authaffil>
      </nde:author>
    </nde:authlist>

  </nde:header>

  <nde:head>
  </nde:head>

  <nde:content>
    <h2 id="introduction">Introduction</h2>

    <p>
      Using HTML forms to process data in web pages can be a complicated 
      affair due to the different representations of the inputs,
      validation, initializing forms, cross-browser. 
      <code>CFormData</code> is a JavaScript<ent:trade/> Object designed to 
      simplify several common tasks when dealing with forms. 
    </p>

    <p>
      <code>CFormData</code> provides:
    </p>
    <ul>
      <li>
        <p>
          The ability to initialize a Form from a the query string used to 
          load a web page.
        </p>
      </li>
      <li>
        <p>
          The ability to initialize itself from the an existing Form 
          and simplify accessing the values.
        </p>
      </li>
      <li>
        <p>
          Generate a query string which can be used to serialize a Form's contents
          or emulate a Form without actually using a <code>Form</code> tag.
        </p>
      </li>
      <li>
        <p>
          Provide a unified means of handing Form processing by tieing the 
          CFormData object to a Form using event handlers and validation functions.
        </p>
      </li>
    </ul>

    <h2>Synopsis</h2>

    <p>
      <code><a href="CFormData.js">CFormData.js</a></code> implements
      the JavaScript Object <code>CFormData</code>.
    </p>

    <dl>
      <dt>Constructor</dt>
      <dd>
        <dl>
          <dt>CFormData()</dt>
          <dd>
            <p>
              Constructs an instance of <code>CFormData</code>.
              <code>CFormData</code> manages a set of named values
              which can correspond to the input elements of a form
              although the named values of the CFormData are <strong>not</strong>
              identical to the input elements of a Form.
            </p>
          </dd>
        </dl>
      </dd>
      <dt>Properties</dt>
      <dd>
        <dl>
          <dt>Object mValueHash</dt>
          <dd>
            <p>
              <code>mValueHash</code> contains the named values 
              managed by this instance of CFormData.
            </p>
          </dd>
          <dt>Array mNames</dt>
          <dd>
            <p>
              <code>mNames</code> is an Array of all of the names 
              managed by the object.
            </p>
          </dd>
          <dt>Object mValidatorHash</dt>
          <dd>
            <p>
              <code>mValidatorHash</code> contains the validation
              functions for each named value.
            </p>
          </dd>
        </dl>
      </dd>
      <dt>Methods</dt>
      <dd>
        <dl>
          <dt>init()</dt>
          <dd>
            <p>
              <code>init()</code> resets the instance of <code>CFormData</code> object to empty.
            </p>
          </dd>
          <dt>String or Array getValue(String aName)</dt>
          <dd>
            <p>
              <code>getValue()</code> returns the value(s) associated with the given 
              name <code>aName</code>. Note <code>getValue()</code>
              will return a single value if the named value has
              only a single value or an <code>Array</code> if 
              multiple values are set.
            </p>
          </dd>
          <dt>setValue(String aName, Object value)</dt>
          <dd>
            <p>
              <code>setValue()</code> sets the value of a named value.
              To set a single valued value, pass a 
              single value in <code>aValue</code>. To set multiple 
              values pass an array of values.
            </p>
          </dd>
          <dt>setValueFromInput(HTMLInputElement input)</dt>
          <dd>
            <p>
              <code>setValueFromInput()</code> sets the value of the named value
              to that of the given input element.
            </p>
          </dd>
          <dt>setValidator(String aName, Function aValidator, String aMessage)</dt>
          <dd>
            <p>
              <code>setValidator</code> sets a validation function
              <code>aValidator</code> for
              the name value <code>aName</code>. <code>aMessage</code>
              is a user message designed to inform the user of the
              correct values for the input.
            </p>
            <p>
              The <code>aValidator</code> function has the same signature as 
              the <code>setValidator</code> method. It returns true if the named
              value is valid, and false otherwise.
            </p>
          </dd>
          <dt>validate(String aName)</dt>
          <dd>
            <p>
              <code>validate()</code> calls the validation function
              for the value named <code>aName</code> returning true
              if value passes the validation and false otherwise.
            </p>
          </dd>
          <dt>getMessage(String aName)</dt>
          <dd>
            <p>
              <code>getMessage()</code> returns the validation
              message for the named value <code>aName</code>.
            </p>
          </dd>
          <dt>getNames()</dt>
          <dd>
            <p>
              <code>getNames()</code> returns an array containing
              the names of all values contained in the object.
            </p>
          </dd>
          <dt>fromString(String aQueryString)</dt>
          <dd>
            <p>
              <code>fromString</code> loads the instance of CFormData from the
              query string into the object.
            </p>
          </dd>
          <dt>fromForm(HTMLFormElement form)</dt>
          <dd>
            <p>
              <code>fromForm()</code> loads the inputs from the 
              specified form into the object.
            </p>
          </dd>
          <dt>toString()</dt>
          <dd>
            <p>
              <code>toString()</code> creates a query string
              corresponding to the values contained in the object.
            </p>
          </dd>
          <dt>toForm(HTMLFormElement form)</dt>
          <dd>
            <p>
              <code>toForm()</code> loads the values in the object
              into the specified form.
            </p>
          </dd>
        </dl>
      </dd>
    </dl>

    <h2 id="example1">Example 1</h2>

    <p>
      <a href="example-1.html">Example 1</a> illustrates the basic use of CFormData to manage
      initializing a form from a query string. Enter values into 
      the form and press submit to see CFormData in action.
    </p>

    <h2 id="example2">Example 2</h2>

    <p>
      <a href="example-2.html">Example 2</a> illustrates a more advanced use of CFormData, including
      synchronizing the values of the CFormData object with the values 
      entered into a Form, and performing validation.
    </p>

    <h2 id="changelog">Change Log</h2>

    <dl>
      <dt>2003-07-14</dt>
      <dd>
        <ul>
          <li>Fixed bug in handling radio, checkbox, selects with only one instance</li>
        </ul>
      </dd>
    </dl>


  </nde:content>

  <nde:related area="nde">
    <nde:item url="/toolbox/examples/2003/WDocumentLoader/">WDocumentLoader</nde:item>
    <nde:item url="/central/dom/">DOM Central</nde:item>
    <nde:item url="/toolbox/examples/">Examples</nde:item>
  </nde:related>
  
</nde:article>
