AkActionViewHelper | --FormHelper
Located in File: /AkActionView/helpers/form_helper.php
The following is an example of a complete form for a person object that works for both creates and updates built with all the form helpers. The <tt>$person</tt> 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>
...is the same as:
<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>
If the object name contains square brackets the id for the object will be inserted. Example:
<?= $form_helper->textfield("person[]", "name") ?>
...becomes:
<input type="text" id="person_<?= $person->id ?>_name" name="person[<?= $person->id ?>][name]" value="<?= $person->name ?>" />
If the helper is being used to generate a repetitive sequence of similar form elements, for example in a partial used by render_collection_of_partials, the "index" option may come in handy. Example:
<?= $form_helper->text_field("person", "name", "index" => 1) ?>
becomes
<input type="text" id="person_1_name" name="person[1][name]" value="<?= $person->name ?>" />
There's also methods for helping to build form tags in $form_options, $date and $active_record
Method check_box (line 228)
assigned to the template (identified by +object+). It's intended that +column_name+ returns an integer and if that integer is above zero, then the checkbox is checked. Additional options on the input tag can be passed as an array with +options+. The +checked_value+ defaults to 1 while the default +unchecked_value+ is set to 0 which is convenient for boolean values. Usually unchecked checkboxes don't post anything. We work around this problem by adding a hidden value with the same name as the checkbox.
Example (call, result). Imagine that $Post->validate() returns 1: $form_helper->check_box("post", "validate"); <input type="checkbox" id="post_validate" name="post[validate]" value="1" checked="checked" /> <input name="post[validated]" type="hidden" value="0" />
Example (call, result). Imagine that $Puppy->gooddog() returns no: $form_helper->check_box("puppy", "gooddog", array(), "yes", "no"); <input type="checkbox" id="puppy_gooddog" name="puppy[gooddog]" value="yes" /> <input name="puppy[gooddog]" type="hidden" value="no" />
Method fields_for (line 146)
<?php $person_form = $this->form_for('person', $Person, array('url' => array('action'=>'update'))); ?> First name: <?= $person_form->text_field('first_name'); ?> Last name : <?= person_form->text_field('last_name'); ?>
<?php $permission_fields = $form_helper->fields_for('permission', $Person->permission); ?> Admin? : <?= $permission_fields->check_box('admin'); ?> <?= $person_form->end_form_tag(); ?>
Note: This also works for the methods in FormOptionHelper and DateHelper that are designed to work with an object as base. Like collection_select and datetime_select.
Method file_field (line 188)
Method form_for (line 124)
<?php $f = $form_helper->form_for('person', $Person, array('url' => array('action' => 'update'))); ?> First name: <?= $f->text_field('first_name'); ?> Last name : <?= $f->text_field('last_name'); ?> Biography : <?= $f->text_area('biography'); ?> Admin? : <?= $f->check_box('admin'); ?> <?= $f->end_form_tag(); ?>
The form_for yields a form_builder object, in this example as $f, which emulates the API for the stand-alone FormHelper methods, but without the object name. So instead of <tt>$form_helper->text_field('person', 'name');</tt>, you get away with <tt>$f->text_field('name');</tt>.
That in itself is a modest increase in comfort. The big news is that form_for allows us to more easily escape the instance variable convention, so while the stand-alone approach would require <tt>$form_helper->text_field('person', 'name', array('object' => $Person));</tt> to work with local variables instead of instance ones, the form_for calls remain the same. You simply declare once with <tt>'person', $Person</tt> and all subsequent field calls save <tt>'person'</tt> and <tt>'object' => $Person</tt>.
Also note that form_for doesn't create an exclusive scope. It's still possible to use both the stand-alone FormHelper methods and methods from FormTagHelper. Example:
<?php $f = $form_helper->form_for('person', $Person, array('url' => array('action' => 'update'))); ?> First name: <?= $f->text_field('first_name'); ?> Last name : <?= $f->text_field('last_name'); ?> Biography : <?= $f->text_area('person', $Biography); ?> Admin? : <?= $form_helper->check_box_tag('person[admin]', $Person->company->isAdmin()); ?> <?= $f->end_form_tag(); ?>
Note: This also works for the methods in FormOptionHelper and DateHelper that are designed to work with an object as base. Like collection_select and datetime_select.
Method hidden_field (line 180)
Method password_field (line 172)
Method radio_button (line 244)
Example (call, result). Imagine that $Post->category() returns "PHP": $form_helper->radio_button("post", "category", "PHP"); $form_helper->radio_button("post", "category", "Ruby"); <input type="radio" id="post_category" name="post[category]" value="PHP" checked="checked" /> <input type="radio" id="post_category" name="post[category]" value="Ruby" />
Method text_area (line 205)
Example (call, result): $form_helper->text_area('post', 'body', array('cols' => 20, 'rows' => 40)); <textarea cols="20" rows="40" id="post_body" name="post[body]"> {post.body} </textarea>
Method text_field (line 164)
Examples (call, result): $form_helper->text_field("post", "title", array("size" => 20)); <input type="text" id="post_title" name="post[title]" size="20" value="{post.title}" />
AkActionViewHelper::$locales_namespace -
AkActionViewHelper::AkActionViewHelper() -
AkActionViewHelper::addObject() -
AkActionViewHelper::getObject() -
AkActionViewHelper::setController() -
AkActionViewHelper::t() -