Marmot Manual

Chapter 11. Forms

Table of Contents

Creating a form
Input item types
Gathering form data
Validating form data
Form caveats

The MU_Form class provides end-to-end services for creating a form, validating a form's contents, and harvesting a form's data. The MU_Inline_Form class provides a subset of the MU_Form class' functionality and is intended to be used to create extremely small and simple forms.

MU_Form forms use the following structure:

  • Form

    • Section

      • Input items

      • Blocks (optional)

        • Input items

      • ...

    • Section

      • Input items

      • Blocks (optional)

        • Input items

      • ...

    • ...

MU_Inline_Form forms use the following simpler structure:

  • Form

    • Input items

The general methodology of using a form is:

  1. Instantiate an MU_Form or MU_Inline_Form object by name

  2. Add sections, blocks, and input items

  3. Add the form to the MU_Presentation object

  4. Marmot handles form validation

  5. Instantiate an MU_Form or MU_Inline_Form object with the same name as the original

  6. Access form data with the value() method

Due to the way Marmot internally handles and passes form data to and from the validation engine, direct access to form data through $_GET, $_POST, and $_REQUEST is not supported. The value() method should be used to extract any user input from the form. When gathering data from a form, you do not need to add the sections, blocks, or input items. The MU_Form class retains all of the information necessary to return data back to the developer.

After creating and populating an MU_Form object, it must be added to the presentation layer with the MU_XML_Element::add_object() method.

$body->add_object($form);

The form must be fully built before adding it to the presentation object. The MU_XML_Element::add_object() method will immediately add the necessary elements to the parent object.

Creating a form

This section includes basic information about how forms are created and added to a document. These examples can also be seen in the "Forms- Creating Forms" example included with Marmot.

A simple form

The following code creates a form with instructions, a textbox, a popup list, and a submit button:

$simple_form = new MU_Form('simple_form'); $simple_form->title = 'Widget Picker (Simple Form)'; $simple_form->instructions = 'Enter your widget specifications below.'; $section =& $simple_form->add_section('widgets'); $section->add_input_item('part_number', array( 'type' => 'numeric', 'label' => 'Part Number', 'size' => '8', 'input_hint'=>'ex: 00007347')); $choices = array( 'red' => 'Red', 'green' => 'Green', 'blue' => 'Blue'); $section->add_input_item('color', array( 'type' => 'list', 'label' => 'Color', 'allow' => 'single', 'list_source' => $choices)); $simple_form->add_user_action('submit', ' Submit ', 'validate'); $body->add_object($simple_form);

We first create a new instance of the MU_Form class, $simple_form.

$simple_form = new MU_Form('simple_form');

We can then set class properties, like the form's action (to what URL it will be submitted), title, and instructions:

$simple_form->title = 'Widget Picker (Simple Form)'; $simple_form->instructions = 'Enter your widget specifications below.';

Forms must be divided into named sections. Our simple form only contains one section, named "widgets:"

$section =& $simple_form->add_section('widgets');

The add_input_item() method is, as the name implies, used to add input items to an enclosing object (either a section or block). In this example, a textbox and a popup menu are added to the section we created above:

$section->add_input_item('part_number', array( 'type' => 'numeric', 'label' => 'Part Number', 'size' => '8', 'input_hint'=>'ex: 00007347')); $choices = array( 'red' => 'Red', 'green' => 'Green', 'blue' => 'Blue'); $section->add_input_item('color', array( 'type' => 'list', 'label' => 'Color', 'allow' => 'single', 'list_source' => $choices));

Refer to Input item types for descriptions of the available form input item types.

The add_input_item() method follows this format:

$enclosing_element->add_input_item('item name', array(attributes), 'default value');

All input items must have a unique name. It is by this name that you will access the submitted form's values. Note that the third argument passed to add_input_item() is a default value. As you will see later in the Validating a form section, Marmot will keep track of a user's input if the form is displayed again.

A user action named "submit" is added to the form with the add_user_action() method. The Presentation Layer displays this as a button. In addition, we tell Marmot to validate the user's input to this form- in this case, checking to make sure that the value of "part_number" is a number.

$simple_form->add_user_action('submit', ' Submit ', 'validate');

Finally, the form is added to the document using the MU_XML_Element::add_object() method:

$body->add_object($simple_form);

Note that the form will not be diplayed if it is not added to the document.

A complicated form

In the following example, we build on our simple form. It now includes a hidden input element, multiple sections, section titles, section-level instructions, blocks within sections, block-level instructions, and input groups:

$complicated_form = new MU_Form('complicated_form'); $complicated_form->title = 'Enhanced Picker (Complicated Form)'; $complicated_form->instructions = 'Please follow the instructions below. Contact the '.mu_insert_element('link',array('href'=>'mailto:sales@widgets.bogus'), 'sales department').' with any questions.'; $complicated_form->add_hidden('transaction_id','w5243'); $section =& $complicated_form->add_section('numeric_fields'); $section->title = 'Widget'; $section->instructions = 'Enter your widget specifications and below.'; $widget_block =& $section->add_block('widget_block'); $widget_block->title = 'Specifications'; $widget_block->instructions = 'Please contact a sales associate for more information or to request a catalog. Be sure to try our exciting new colors, blue and green!'; $widget_block->add_input_item('part_number', array( 'type' => 'numeric', 'label' => 'Part Number', 'size' => '8', 'input_hint'=>'ex: 00007347')); $choices = array( 'red' => 'Red', 'green' => 'Green', 'blue' => 'Blue'); $widget_block->add_input_item('color', array( 'type' => 'list', 'label' => 'Color', 'allow' => 'single', 'list_source' => $choices)); $section =& $complicated_form->add_section('field_groups'); $section->title = 'Buyer'; $name =& $section->add_input_item('name_group', array('type' => 'field_group', 'label' => 'Name')); $name->add_group_member('first_name', array('type' => 'text', 'name' => 'first_name', 'label' => 'First')); $name->add_group_member('mi', array('type' => 'text', 'name' => 'mi', 'label' => 'MI', 'size' => '2', 'maxlength' => '2')); $name->add_group_member('last_name', array('type' => 'text', 'name' => 'last_name', 'label' => 'Last')); $name->instructions = 'Please enter your name'; $section =& $complicated_form->add_section('text_field'); $section->title = 'Special Instructions'; $large =& $section->add_input_item('special_instructions', array('type' => 'large_text', 'columns' => '50', 'rows' => '5')); $large->instructions = 'Please enter any special instructions here'; $complicated_form->add_user_action('submit', ' Submit ', 'validate'); $body->add_object($complicated_form);

An inline form

The MU_Inline_Form class is used to create tiny, extremely simple forms. It is used almost exactly like MU_Form, but doesn't include support for such features as sections, blocks, or validation.

$inline_form = new MU_Inline_Form('inline_form'); $inline_form->add_input_item('part_number', array( 'type' => 'text', 'label' => 'Part Number', 'size' => '8')); $inline_form->add_user_action('search', ' Search '); $body->add_object($inline_form);

Hidden input elements

It is sometimes necessary to insert elements into a form that are submitted along with a user's input, but are not displayed. In HTML, these are "input" items of the type "hidden". Marmot allow you to insert such elements by invoking the MU_Form class's add_hidden method:

$form->add_hidden('element_name', $element_value);

Form actions

By default, all types of forms submit to the page that created them. This allows the developer to keep the data process in the same file as the form definition. If you want to change the page the form data is submitted to, simply alter the action attribute of the form to a valid URL (relative to the current page). For example:

$form = new MU_Form('my_form'); $form->action = "form_handler.php";