www.KevinBurkholder.com

Getting Work Done Through People

Getting People Done Through Work

www.KevinBurkholder.com
Strengths Based Performance Management
EarthAsylum Consulting
The EarthAsylum Leadership Circle
The EarthAsylum Fusion Network
Wednesday, November 26, 2008

eac_formgen.class.php Form generator

eac_formgen.class.php

Source Code

Documentation (42pg/996k)

Reference Tables (9pg/292k)

Test Form Example (Source)

Interested in using this software in your project? Contact Kevin at KBurkholder@EarthAsylum.com

Overview

Form Generator, as its name implies, is a PHP utility used to generate html forms within a web page. Forms generated by Form Generator contain very detailed fields and fieldsets with complete name, id, and class markup for wide-ranging layout customization.

Form Generator does not do layout. It generates the form, fields and fieldsets only. The only layout specified by Form Generator is the order of the fields, which is dictated by the application using Form Generator.

eac_formgen.class features...

  • Dynamically generated forms.
  • Complete custom layout (fields and format).
  • 25 different field types and 30 different field parameters.
  • Field validation with over 30 optional rules.
  • Custom event triggers at the field level.
  • Ability to synchronize multiple fields.
  • Hide/show field(s) based on input.

There are 25 different types of input fields:

  • Text - A text input field
  • Disabled - A disabled text input field
  • Password - A password field (entry is echoed as “*”)
  • TextArea - A text block
  • ReadOnly - A text block which cannot be changed
  • File - A file upload field (Upload alias)
  • Upload - A file upload field
  • Hidden - A Hidden field
  • Display - Displays the value in the ‘Text’ option.
  • Button - A button (use ‘Events’ to assign an action)
  • ButtonImg - A button with an image.
  • Submit - A form submit button
  • Reset - A form reset button
  • Image - An image button (use ‘Events’ to assign an action)
  • Radio - A series of radio buttons
  • RadioImg - Radio buttons with images
  • Checkbox - A series of check boxes
  • CheckboxImg - Checkboxes with images
  • Select - A drop-down select list
  • SelectM - A drop down select, which allows multiple selections
  • Date - A date field with 3 fields (m d y)
  • Phone - A phone field with 3 fields (###) ###-###
  • State - A select drop-down of US states and/or Canadian provinces
  • SmState - A select drop-down of US or Canadian state/province abbreviations.
  • Expire - A month/year expire date drop-down with 2 fields (m y)

There are 30 different parameters that may be applied to fields:

  • Type - the type of field (see above).
  • Text - the label/legend for the field.
  • Required - is it a required field?
  • Default - the default value.
  • Format - specify a format string used when displaying the field.
  • When - only show field when - (variable,[==,!=,<,>,<=,>=],"value").
  • Label - label (when different from ['Text']).
  • Legend - legend (when different from ['Text']).
  • Before - text before the field.
  • After - text after the field.
  • Size - size/max-size or rows/cols.
  • Choice - radio/checkbox/select choices ("something"=>value).
  • FileTypes - file types (for type=upload).
  • Src - image src= (for type=image).
  • Content - readonly content (depreciated, use default).
  • Events - array of event=action (i.e. onclick=>action).
  • Class - assign a CSS class to the field's fieldset.
  • Style - applies in-line style to the field's fieldset.
  • Flags -
    • useLabel - use/don’t use a label for this field.
    • useLegend - use/don’t use a legend for this field.
    • disabled - disable this field.
    • onlySelected - only show selected radio & checkbox options (when disabled).
  • Valid - array of validation tests (required,alpha,numeric...).
  • Error - error message used when field fails validation.
  • Hint - Display a hint or tool-tip.
  • Repeat - Allow the field to be repeated.
  • SwitchOn - Hide or show this field based on another field.
  • Switch - Hide or show another field based on this field.
  • SyncTo - Copy this field to another field.
  • SyncFrom - copy another field to this field.
  • Fields - embedded fields create a new fieldset

A fieldset is a container that contains fields. A field itself is a fieldset that may contain a legend (or header), label, the input field, any items to include before or after the field, a hint (or tool-tip) value, and an error value.

Fieldsets can contain fieldsets that can contain fields. There’s no limit to the depth of a fieldset although the deeper the more complicated it is to style.

The layout of a form is defined by styling the fieldsets and fields within the form. Each element of a field can be referenced by the style sheet and all available styles can be attributed to any element in a field.

Usage Summary

Form Generator consists of the primary class, eac_formgen.class.php, an included file used to generate client-side JavaScript code, form_gen,class,js.php and the JavaScript foundation libraries provided by wForms (see wForms later in this document). An optional custom program, eac_formgen.post.php, is used for Ajax calls to capture and/or validate forms as the field data is entered or just prior to a form submit.

First, include the class library in your php document:

require_once('eac_formgen.class.php');

Next create an instance of the form_generator class:

$myform = new form_generator(array(
            'uses'=>'hint,repeat,switch,paging,validation,onblur,sync')
            );

Or

$myform = new form_generator();
$myform->preProcess($fields);

* These two steps should be executed within the “<head>” portion of the document.

To start creating a form, call the “startForm” method:

$myform->startForm(array(
               'form' => "MyFormName",
               'id' => "MyFormID",
               'action' => $PHP_SELF,
               'method' => "post",
               'enctype' => "multipart/form-data",
            )
         );

Next, create fieldsets and fields:

$myform->startFieldset('MyFieldSet','MyFieldSet Legend');
$myform->addField($fname,$foptions);
$myform->addField($fname,$foptions);
$myform->addField($fname,$foptions);
                                    ...
$myform->endFieldset();

Lastly, end the form:

$myform->endForm();

A form should always start with at least one fieldset and this will become the parent fieldset for all of the fields.

A typical method for generating fields is to define the fields in an array and call addField() in a foreach() loop.

$fields = array();
$fields['First_Field'] = array(
            'Type' => "Text",
            'Text' => "First Field",
            'Size' => array(20,30),
            'Default' => "default text",
            'Hint' => "This field is required",
            'Required' => true
);
$fields['Second_Field'] = array(
            ...
$fields['Third_Field'] = array(
            ...
foreach($fields as $fname => $fopt) {
            $myform->addField($fname,$fopt);
}

Methods

  • startForm()
  • startFieldset()
  • addField()
  • replicateFields()
  • endFieldset()
  • storePart()
  • endForm()
  • getForm()
  • getPart()

wForms

“wForms is an open-source, unobtrusive JavaScript library that adds commonly needed behaviors to traditional web forms”

http://www.formassembly.com/wForms/

As stated above, wForms is a JavaScript library (actually a number of libraries or foundations) used to enhance the functionality of a typical web form.

wForms adds the capability to…

  • Synchronize fields.
  • Show tool-tip hints for fields.
  • Repeat or duplicate fields and fieldsets.
  • Create conditional fields based on prior field responses.
  • Validate fields at time of entry and when the form is submitted.
  • Create multiple page forms.

Form Generator incorporates and enhances many of the wForms functions, delivers the needed JavaScript when the class is instantiated, and tags all fields and fieldsets with the needed code to fully support wForms.

As a JavaScript library, wForms runs on the client browser based on the assignments given by Form Generator and on actions taken by the person using the form.

wForms works through the extensive use of style class names. Not just for applying styles to the form but more so for detecting form elements for actions such as validation. Form generator will automatically insert the correct class names and parameters for wForms based on the field and fieldset options.

 Powered by  eac::Framework 

eac::Framework is a lightweight PHP & JavaScript framework for Web 2.0 Applications and E-Commerce systems.

For more information, visit http://www.KevinBurkholder.com/framework

eac::encryption, eac::session, eac::keychain, eac::dataobjects, eac::tracker, eac::sourcing, eac::authentication, eac::filter, eac::formgen, eac::caching, eac::mailer, eac::download, eac::error, eac::streams and more.