Form Helpers

Form Helpers

Form Helpers are Akelos functions designed to make the code that produces web forms more streamlined and easier to read. Sometimes a Helper will be used instead of an HTML/PHP statement. At other times, a Helper will be a single statement that would require a number of HTML/PHP statements to write without it.

A form built without Akelos Form Helpers might look like:

<form action="save_person" method="post"> 
Name: <input type="text" id="person_name" name="person[name]" size="20" value="<?= $person->name ?>" />
Password: <input type="password" id="person_password" name="person[password]" 
    size="20" maxsize="20" value="<?= $person->password ?>" />
Single?: <input type="checkbox" id="person_single" name="person[single]" value="1" />
Description: <textarea cols="20" rows="40" id="person_description" name="person[description]"> 
    <?= $person->description ?> </textarea>
<input type="submit" value="Save"> 
</form>

The following is an example of the same form built with Akelos Stand Alone Form Helpers. The $person object was assigned by an action on the controller:

<form action="save_person" method="post">
Name: <?= $form_helper->text_field("person", "name", array("size" => 20)) ?>
Password: <?= $form_helper->password_field("person", "password", array("maxsize" => 20)) ?>
Single?: <?= $form_helper->check_box("person", "single") ?>
Description: <?= $form_helper->text_area("person", "description", array("cols" => 20)) ?>
<input type="submit" value="Save" /> 
</form>

Here are other features that you can put into Helpers:

Object name followed by square brackets creates a reference to the object's id field. Without a Helper, you'd have to write

<input type="text" id="person_<?= $person->id ?>_name" 
    name="person[<?= $person->id ?>][name]" value="<?= $person->name ?>" />

You may write the same code with this Helper:

<?= $form_helper->textfield("person[]", "name") ?>

If the Helper is being used to generate a repetitive sequence of similar form elements, the “index” option may be used. Without the Helper, you'd have to write

<input type="text" id="person_1_name" name="person[1][name]" value="<?= $person->name ?>" />

render_collection_of_partials needs to be referenced or documented here.

This Helper, used in conjunction with the Helper render_collection_of_partials would look like

<?= $form_helper->text_field("person", "name", "index" => 1) ?>

Form Tag Helpers

In this section, we will look at the functions normally used to build a form. There are three ways to write forms code. A form may use any of them, or a combination of them:

  1. No helper When creating a form, it is possible to write the HTML/PHP code without any Akelos Form Helpers. Those who have programmed PHP without Akelos will be familiar with these. We showed a form written this way in the first example.
  2. Stand Alone Helper The reason for the Akelos Stand Alone Helper designation is that this type does not reference the form declaration. Reference to a controller object is made from each Helper of this type. They may be used with a non-Akelos form declaration or with a Helper form declaration. If used with a Helper form declaration, the object they refer to may be different from that which is referred to by the form declaration.
  3. Form Tag Helper This designation is given because this type must be coded between the form_for and end_form_tag functions.

Following is a reference to the form tools. More can be seen about the HTML code at w3schools and other HTML web sites.

HTML Helper Comment
form form_for Wrapper for form fields; defines form
fields_for Wrapper for form fields; does not define form
input type=“text” text_field
input type=“file” file_field Input for a file name with a browse button
input type=“hidden” hidden_field
input type=“password” password_field
input type=“radio” radio_button
checkbox check_box
textarea text_area
select select
 
forms.txt · Last modified: 2008/02/28 14:12 by alake
 

The Akelos Framework was created by Bermi Ferrer and other contributors.
Potions of the code and documentation have been ported from Ruby on Rails.

The Akelos Framework is released under the LGPL license.

"Akelos", "Akelos Framework", and the Akelos logo are trademarks of Bermi Labs All rights reserved.

Wiki driven by DokuWiki