Form For

Table of Contents

form_for

Syntax:

$form_helper->form_for( $object_name, &$object, [ $options ])

Description:
Within the scope of the form_for ... end_form_tag functions, field declarations do not require the object name. Instead of writing

$form_helper->text_field('person', 'name');

you may write

$f->text_field('name');

Another advantage to using form_for is that instance variables may be replaced with local variables without changing the code. While a Stand Alone Helper using a local variable would have to be written

$form_helper->text_field('person', 'name', array('object' => $Person));

the Helpers that may be used with form_for remains the same as with an instance variable. You simply declare form_for once with 'person', $Person and all Helpers for fields within its scope implies the 'person' and 'object' => $Person.

Also note that the scope of form_for isn't exclusive. It's still possible to use the Stand Alone Form Helpers as well as the Form Tag Helpers. In this example, the check_box_tag is out of the scope of the other field declarations.
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. This note needs more of an explanation and also references.

form_tag

Syntax:

$form_tag_helper->form_tag( [ $url_for_options ], [ $options ])

Description:
Starts a form tag that points the action to a URL configured with the url_for_options array just like $controller→urlFor. The method for the form defaults to POST.
$options is an array:

  • 'multipart' - If set to true, the enctype is set to “multipart/form-data”.
  • 'method' - The method to use when submitting the form, usually either “get” or “post”.

start_form_tag

Syntax:

$form_tag_helper->start_form_tag([ $url_for_options ],[ $options ])

Description:
Starts a form tag that points the action to a URL configured with the url_for_options array just like $controller→urlFor.
$options is an array:

  • 'multipart' - If set to true, the enctype is set to “multipart/form-data”.
  • 'method' - The method to use when submitting the form, usually either “get” or “post”.
 
helper-form.txt · Last modified: 2008/03/07 00:12 by alake