6.7. Semi-Automatic View Presentation

The auto() method is useful for displaying all the elements in a form object at once, but frequently we need to show only some of the elements, sometimes as part of a fieldset or a group. For this, we will need to use other form() helper methods instead of the auto() method, and retrieve individual elements from the form object.

For this section, let's imagine we have a form object from a hypothetical Acme_Model_Users_Record with the following elements:

<?php
$this->form->elements['user[handle]']      = array(...);
$this->form->elements['user[moniker]']     = array(...);
$this->form->elements['user[name_first]']  = array(...);
$this->form->elements['user[name_middle]'] = array(...);
$this->form->elements['user[name_last]']   = array(...);
$this->form->elements['user[email]']       = array(...);
$this->form->elements['user[phone]']       = array(...);

6.7.1. Fieldsets

Let's imagine that it is a requirement for this view that we present the name portions in a fieldset, and the contact portions in a separate fieldset.

<?php
echo $this->form()
          ->meta($this->form)
          ->beginFieldset('LEGEND_NAME')
          ->addElements($this->form->getElements('user[name_'))
          ->endFieldset()
          ->beginFieldset('LEGEND_CONTACT')
          ->addElements($this->form->getElements(array(
                'user[email]',
                'user[phone]',
            )))
          ->endFieldset()
          ->addProcess('save')
          ->fetch();

The meta() method gets the form status, the form tag attributes, and the feedback for the form as a whole (i.e. the top-level feedback messages like "Success" or "Failure".)

The beginFieldset() and endFieldset() methods add opening and closing fieldset tags, respectively.

The addElements() method adds only the elements listed. The complementary Solar_Form::getElements() method retrieves elements from the form object. If you pass a string argument, it will return all elements whose name begins with that string; if you pass an array of element names, it will return only those elements.

The output generated by that view script code will look something like the following.

<form method="post" enctype="multipart/form-data">
    <fieldset>
        <legend>LEGEND_NAME</legend>
        <dl class="list">
            <dt class="user-name_first">
                <label for="user-name_first" class="user-name_first">LABEL_NAME_FIRST</label>
            </dt>
            <dd class="user-name_first">
                <input type="text" name="user[name_first]" value="" id="user-name_first" 
                    class="input-text user-name_first"
                />
            </dd>
            <dt class="user-name_middle">
                <label for="user-name_middle" class="user-name_middle">LABEL_NAME_MIDDLE</label>
            </dt>
            <dd class="user-name_middle">
                <input type="text" name="user[name_middle]" value="" id="user-name_middle" 
                    class="input-text user-name_middle"
                />
            </dd>
            <dt class="user-name_last">
                <label for="user-name_last" class="user-name_last">LABEL_NAME_LAST</label>
            </dt>
            <dd class="user-name_last">
                <input type="text" name="user[name_last]" value="" id="user-name_last" 
                    class="input-text user-name_last" 
                />
            </dd>
        </dl>
    </fieldset>
    <fieldset>
        <legend>LEGEND_CONTACT</legend>
        <dl class="list">
            <dt class="user-email">
                <label for="user-email" class="user-email">Email</label>
            </dt>
            <dd class="user-email">
                <input type="text" name="user[email]" value="" id="user-email" 
                    class="input-text user-email"
                />
            </dd>
            <dt class="user-phone">
                <label for="user-phone" class="user-phone">LABEL_PHONE</label>
            </dt>
            <dd class="user-phone">
                <input type="text" name="user[phone]" value="" id="user-phone" 
                    class="input-text user-phone" 
                />
            </dd>
        </dl>
    </fieldset>
    <dl class="list">
        <dt class="process-save">
            
        </dt>
        <dd class="process-save">
            <input type="submit" name="process" value="Save" id="process-save" 
                class="input-submit process-save"
            />
        </dd>
    </dl>
</form>

6.7.2. Element Groups

Whereas a fieldset is a series of separate elements with their own labels, a group is a series of elements under a single label. Let's say we want to have the name elements grouped under one label:

<?php
echo $this->form()
          ->meta($this->form)
          ->beginGroup('LABEL_NAME')
          ->addElements($this->form->getElements('user[name_'))
          ->endGroup()
          ->addProcess('save')
          ->fetch();

That view script code will generate output similar to the following:

<form method="post" enctype="multipart/form-data">
    <dl class="list">
        <dt class="label">
            <label>LABEL_NAME</label>
        </dt>
        <dd class="value">
            <input type="text" name="user[name_first]" value="" id="user-name_first" 
                class="input-text user-name_first" 
            />
            <input type="text" name="user[name_middle]" value="" id="user-name_middle" 
                class="input-text user-name_middle" 
            />
            <input type="text" name="user[name_last]" value="" id="user-name_last" 
                class="input-text user-name_last" 
            />
        </dd>
        <dt class="process-save">
            
        </dt>
        <dd class="process-save">
            <input type="submit" name="process" value="Save" id="process-save" 
                class="input-submit process-save" 
            />
        </dd>
    </dl>
</form>


Local