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) ?>
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:
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 |