|
::Features
::Download
::Tutorial
::Examples
::Documentation ::FormBuilder Google Group ::Old List Archives ::Contribute |
|
Intermediate Tutorial - Step 3: Default Values Intelligent handling of defaults is another one of FormBuilder's real strengths. It's another painful task that FormBuilder makes very easy. Let's assume you want to create a form that allows a customer to update their contact information, as basically every company must do. This info is stored in a database, and you want to create an easy interface to it. Doing so is easy:
#!/usr/bin/perl
use DBI;
use CGI::FormBuilder;
$user = $ENV{REMOTE_USER}; # from .htaccess
$dbh = DBI->connect(...); # your db here
$sth = $dbh->prepare("select * from pers where user = $user");
$defs = $sth->fetchrow_hashref;
@fields = qw(first_name last_name email phone
address city state zip mail_list);
$form = CGI::FormBuilder->new(
method => 'post',
fields => \@fields,
values => $defs, # values from hashref
required => 'ALL'
);
# Setup attributes for some fields
$form->field(name => 'state', options => \@states);
$form->field(name => 'zip', size => 10, maxlength => 10);
$form->field(name => 'mail_list', options => [qw/Yes No/]);
# No confirmation to demonstrate stickiness
print $form->render(header => 1);
By adding the values option, FormBuilder will walk the
hashref specified, search it case-insensitively for matching fields,
and automatically populate your form. The correct select
options will be selected, checkboxes checked, and so on.
You should really see this in action before continuing. Notice how the CGI values override the defaults from the database across multiple submissions. |
| FormBuilder is © Nate Wiger, with contributions from many people. |