Perl Form Builder ::Features ::Download ::Tutorial ::Examples
::Documentation ::FormBuilder Google Group ::Old List Archives ::Contribute
Note: The mailing list is has been replaced by the Google FormBuilder Group

Basic

1. Intro to FormBuilder
2. A Simple Form
3. Form Method
4. Input Validation
5. Field Options
6. Multi-Select Lists
7. Field Data
8. Field Attributes
9. HTML Formatting
10. Custom Headers
S. Summary

Intermediate

1. Trickier Forms
2. Perl References
3. Default Values
4. CGI Stickiness
5. JavaScript Actions
6. Input Validation 2
7. Multiple Submits
8. JavaScript Actions 2
9. Modules & mod_perl
10. Smartness
S. Summary

Advanced

1. Remaining Features
2. Intro to Templates
3. HTML::Template
4. HTML::Template 2
5. Template Toolkit
6. Exploiting render()
7. Multi-Screen Apps
8. Multi-Screen Apps 2
9. Multi-Screen Apps 3
10. Custom Messages
S. Summary

Intermediate Tutorial - Step 3: Default Values Print

<< Perl References | CGI Stickiness >>

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.

<< Perl References | CGI Stickiness >>

FormBuilder is © Nate Wiger, with contributions from many people.
Nateware