Class FormHelper

(line 87)

Description

AkActionViewHelper
   |
   --FormHelper

Located in File: /AkActionView/helpers/form_helper.php

Provides a set of methods for working with forms and especially forms related to objects assigned to the template.

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



Classes extended from FormHelper:
AkFormHelperBuilder
Provides a set of methods for working with forms and especially forms related to objects assigned to the template.

Class Variables

Summary:

Class Constants

Summary:

Method Detail

Summary:
void check_box ( $object_name, [ $column_name = null], [ $options = array()], [ $checked_value = '1'], [ $unchecked_value = '0'])
void end_form_tag ()
void fields_for ( $object_name,  &$object)
void file_field ( $object_name, [ $column_name = null], [ $options = array()])
void form_for ( $object_name,  &$object, [ $options = array()])
void hidden_field ( $object_name, [ $column_name = null], [ $options = array()])
void password_field ( $object_name, [ $column_name = null], [ $options = array()])
void radio_button ( $object_name, [ $column_name = null],  $tag_value, [ $options = array()])
void text_area ( $object_name, [ $column_name = null], [ $options = array()])
void text_field ( $object_name, [ $column_name = null], [ $options = array()])

Method check_box (line 228)

void check_box( $object_name, [ $column_name = null], [ $options = array()], [ $checked_value = '1'], [ $unchecked_value = '0'])

Returns a checkbox tag tailored for accessing a specified attribute (identified by +column_name+) on an object

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" />

Parameters

  • $object_name:
  • $column_name:
  • $options:
  • $checked_value:
  • $unchecked_value:

Info

Method end_form_tag (line 151)

void end_form_tag( )

Info

Method fields_for (line 146)

void fields_for( $object_name, &$object)

Creates a scope around a specific model object like form_for, but doesn't create the form tags themselves. This makes fields_for suitable for specifying additional model objects in the same form. Example:

<?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.

Parameters

  • $object_name:
  • &$object:

Info

Method file_field (line 188)

void file_field( $object_name, [ $column_name = null], [ $options = array()])

Works just like text_field, but returns an input tag of the "file" type instead, which won't have a default value.

Parameters

  • $object_name:
  • $column_name:
  • $options:

Info

Method form_for (line 124)

void form_for( $object_name, &$object, [ $options = array()])

Creates a form and a scope around a specific model object, which is then used as a base for questioning about values for the fields. Examples:

<?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.

Parameters

  • $object_name:
  • &$object:
  • $options:

Info

Method hidden_field (line 180)

void hidden_field( $object_name, [ $column_name = null], [ $options = array()])

Works just like text_field, but returns an input tag of the "hidden" type instead.

Parameters

  • $object_name:
  • $column_name:
  • $options:

Info

Method password_field (line 172)

void password_field( $object_name, [ $column_name = null], [ $options = array()])

Works just like text_field, but returns an input tag of the "password" type instead.

Parameters

  • $object_name:
  • $column_name:
  • $options:

Info

Method radio_button (line 244)

void radio_button( $object_name, [ $column_name = null], $tag_value, [ $options = array()])

Returns a radio button tag for accessing a specified attribute (identified by +column_name+) on an object assigned to the template (identified by +object+). If the current value of +column_name+ is +tag_value+ the radio button will be checked. Additional options on the input tag can be passed as an array with +options+.

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" />

Parameters

  • $object_name:
  • $column_name:
  • $tag_value:
  • $options:

Info

Method text_area (line 205)

void text_area( $object_name, [ $column_name = null], [ $options = array()])

Returns a textarea opening and closing tag set tailored for accessing a specified attribute (identified by +column_name+) on an object assigned to the template (identified by +object+). Additional options on the input tag can be passed as an array with +options+.

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>

Parameters

  • $object_name:
  • $column_name:
  • $options:

Info

Method text_field (line 164)

void text_field( $object_name, [ $column_name = null], [ $options = array()])

Returns an input tag of the "text" type tailored for accessing a specified attribute (identified by +column_name+) on an object assigned to the template (identified by +object+). Additional options on the input tag can be passed as an array with +options+.

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}" />

Parameters

  • $object_name:
  • $column_name:
  • $options:

Info

Inherited Variables

Inherited Class Variable Summary

Inherited From Class AkActionViewHelper

AkActionViewHelper::$locales_namespace -

Inherited Methods

Inherited Method Summary

Inherited From Class AkActionViewHelper

AkActionViewHelper::AkActionViewHelper() -

AkActionViewHelper::addObject() -

AkActionViewHelper::getObject() -

AkActionViewHelper::setController() -

AkActionViewHelper::t() -



Documentation generated on Sat, 20 Oct 2007 00:23:22 +0200 by phpDocumentor 1.3.2