6.5. View Presentation

At this point, we have used controller logic to work with a model record, and asked the record to give us a form object. Now we need to display that form object in a view. Solar has a whole series of view helpers for rendering forms manually, but here we are going to concentrate on automated form presentation using Solar_View_Helper_Form, which you access using $this->form() in a view script.

[Note] Note

Solar_View_Helper_Form uses "fluent" methods for most of its operation. This means you can chain methods together in a single call to perform a series of operations in sequence.

The easiest way to display a form is to use $this->form()->auto(). This method will take a Solar_Form object and build out its elements automatically for us. We have seen this previously in the blog demo for edit.php view.

    echo $this->form()             // fluent form helper
              ->auto($this->form)  // auto-build from a Solar_Form object
              ->addProcess('save') // add a "Save" button
              ->fetch();           // fetch the xhtml output
[Note] Note

What's with the call to addProcess()? The model record newForm() method does not add submit buttons, since it doesn't necessarily know how the form is going to be used by the calling logic. As such, we add submit buttons with process keys at view time; the process keys allow the controller to know which submit button was clicked by the the end-user.

Those four lines of view script code build the form for you, complete with CSS classes and IDs based on the form element types, names, attributes, whether or not they are required, whether or not they are invalid, and so on.

<form action="/blog/edit/2" method="post" enctype="multipart/form-data">
    <dl class="list">
        <dt class="blog-status">
            <label for="blog-status" class="blog-status">Status</label>
        <dd class="blog-status">
            <select name="blog[status]" id="blog-status" class="input-select blog-status">
                <option value="draft" label="draft" selected="selected">draft</option>
                <option value="public" label="public">public</option>
        <dt class="blog-title require">
            <label for="blog-title" class="blog-title require">Title</label>
        <dd class="blog-title require">
            <input type="text" name="blog[title]" value="A Draft Article" 
                maxlength="63" size="60" id="blog-title" class="input-text 
                blog-title require"
        <dt class="blog-body">
            <label for="blog-body" class="blog-body">Body</label>
        <dd class="blog-body">
            <textarea name="blog[body]" rows="18" cols="60" id="blog-body" 
                class="input-textarea blog-body">My, it sure is drafty in here.</textarea>
        <dt class="process-save">
        <dd class="process-save">
            <input type="submit" name="process" value="Save" id="process-save"
                class="input-submit process-save" />