[ Index ]

PHP Cross Reference of Akelos Framework

title

Body

[close]

/AkActionView/helpers/ -> form_helper.php (summary)

(no description)

Author: Bermi Ferrer
Copyright: Copyright (c) 2002-2006, Akelos Media, S.L. http://www.akelos.org
License: GNU Lesser General Public License
File Size: 526 lines (22 kb)
Included or required: 3 times
Referenced: 0 times
Includes or requires: 4 files
 AkInflector.php
 AkActionView/helpers/tag_helper.php
 AkActionView/helpers/date_helper.php
 AkActionView/helpers/form_tag_helper.php

Defines 3 classes

FormHelper:: (11 methods):
  form_for()
  fields_for()
  end_form_tag()
  text_field()
  password_field()
  hidden_field()
  file_field()
  text_area()
  check_box()
  radio_button()
  _field()

AkFormHelperInstanceTag:: (18 methods):
  AkFormHelperInstanceTag()
  to_input_field_tag()
  to_radio_button_tag()
  to_text_area_tag()
  to_check_box_tag()
  to_date_tag()
  to_date_select_tag()
  to_datetime_select_tag()
  to_boolean_select_tag()
  to_content_tag()
  getObject()
  getValue()
  value_before_type_cast()
  add_default_name_and_id()
  tag_name()
  tag_name_with_index()
  tag_id()
  tag_id_with_index()

AkFormHelperBuilder:: (1 method):
  AkFormHelperBuilder()


Class: FormHelper  - X-Ref

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
form_for($object_name, &$object, $options = array()   X-Ref
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.

fields_for($object_name, &$object)   X-Ref
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.

end_form_tag()   X-Ref
No description

text_field($object_name, $column_name = null, $options = array()   X-Ref
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}" />

password_field($object_name, $column_name = null, $options = array()   X-Ref
Works just like text_field, but returns an input tag of the "password" type instead.


hidden_field($object_name, $column_name = null, $options = array()   X-Ref
Works just like text_field, but returns an input tag of the "hidden" type instead.


file_field($object_name, $column_name = null, $options = array()   X-Ref
Works just like text_field, but returns an input tag of the "file" type instead, which won't have a default value.


text_area($object_name, $column_name = null, $options = array()   X-Ref
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>

check_box($object_name, $column_name = null, $options = array()   X-Ref
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" />

radio_button($object_name, $column_name = null, $tag_value, $options = array()   X-Ref
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" />


_field($object_name, $column_name, $options = array()   X-Ref
File field auxiliar function


Class: AkFormHelperInstanceTag  - X-Ref

AkFormHelperInstanceTag($object_name, $column_name, &$template_object, $local_binding = null, $object = null)   X-Ref
No description

to_input_field_tag($field_type, $options = array()   X-Ref
No description

to_radio_button_tag($tag_value, $options = array()   X-Ref
No description

to_text_area_tag($options = array()   X-Ref
No description

to_check_box_tag($options = array()   X-Ref
No description

to_date_tag()   X-Ref
No description

to_date_select_tag($options = array()   X-Ref
No description

to_datetime_select_tag($options = array()   X-Ref
No description

to_boolean_select_tag($options = array()   X-Ref
No description

to_content_tag($tag_name, $options = array()   X-Ref
No description

getObject($object_name = null)   X-Ref
No description

getValue()   X-Ref
No description

value_before_type_cast()   X-Ref
No description

add_default_name_and_id(&$options)   X-Ref
No description

tag_name()   X-Ref
No description

tag_name_with_index($index)   X-Ref
No description

tag_id()   X-Ref
No description

tag_id_with_index($index)   X-Ref
No description

Class: AkFormHelperBuilder  - X-Ref



Generated: Mon Oct 27 12:43:49 2008 Cross-referenced by PHPXref 0.6