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

Advanced Tutorial - Step 8: Multi-Screen Apps 2 Print

<< Multi-Screen Apps | Multi-Screen Apps 3 >>

This section will examine the first alternative:

  1. Create a separate CGI.pm $query object, use it to get your params, and then create your $form objects with the params option.
If you remember way back from the Basic tutorial, we already outlined pretty much how to do this in the "Custom Headers" section. Let's assume that the parameter is called mode. The only extra option we have to add is called keepextras, which will maintain non-field params as hidden fields across submissions. Otherwise, our mode will seem to magically "disappear" when we submit the form:
#!/usr/bin/perl

# article.pl - show, add, edit, delete online articles

use strict;       # recommended but not required

use CGI;
use CGI::FormBuilder;

my $query = new CGI;                            # OO style
my $mode  = $query->param('mode') || 'show';    # get the mode

if ($mode eq 'show') {
    # Show existing article
    my $article = $query->param('article_id');


} elsif ($mode eq 'new') {
    # New article
    my $form = CGI::FormBuilder->new(
                    fields => [qw/title author post_date summary
                                  details city country/],

                    params => $query,           # params from CGI.pm 
                    keepextras => 1             # keep mode param 
               );

    if ($form->submitted && $form->validate) {
        # post new article
    } else {
        print $form->render(template => '/templates/new_article.tmpl');
    }

} elsif ($mode eq 'edit') {
    # Edit article ...


} elsif ($mode eq 'delete') {
    # Delete article ...


}
This code example could go for pages and pages. Basically, each elsif statement is its own component. The components are switched between with the special mode parameter. Inside each component, a separate $form object is created to allow us to customize the fields and templates for each action.

You'll find this type of methodology most useful in applications like this one or in Apache mod_perl modules. In mod_perl modules, it's often easiest to have a different sub for each mode, and then the main handler sub simply redispatches. Each action sub would generate its own $form and output.

<< Multi-Screen Apps | Multi-Screen Apps 3 >>

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