CGI-FormBuilder
view release on metacpan or search on metacpan
lib/CGI/FormBuilder.pod view on Meta::CPAN
fields => [
qw(first_name last_name email
address state zipcode)
],
header => 1,
debug => 2, # gook
required => 'NONE',
);
# This adds on the 'details' field to our form dynamically
$form->field(name => 'details',
type => 'textarea',
cols => '50',
rows => '10');
# And this adds user_name with validation
$form->field(name => 'user_name',
value => $ENV{REMOTE_USER},
validate => 'NAME');
if ($form->submitted && $form->validate) {
# ... more code goes here to do stuff ...
print $form->confirm;
} else {
print $form->render;
}
In this case, none of the fields are required, but the C<user_name>
field will still be validated if filled in.
=head2 Ex3: ticket_search.cgi
This is a simple search script that uses a template to layout
the search parameters very precisely. Note that we set our
options for our different fields and types.
#!/usr/bin/perl
use strict;
use CGI::FormBuilder;
my $form = CGI::FormBuilder->new(
fields => [qw(type string status category)],
header => 1,
template => 'ticket_search.tmpl',
submit => 'Search', # search button
reset => 0, # and no reset
);
# Need to setup some specific field options
$form->field(name => 'type',
options => [qw(ticket requestor hostname sysadmin)]);
$form->field(name => 'status',
type => 'radio',
options => [qw(incomplete recently_completed all)],
value => 'incomplete');
$form->field(name => 'category',
type => 'checkbox',
options => [qw(server network desktop printer)]);
# Render the form and print it out so our submit button says "Search"
print $form->render;
Then, in our C<ticket_search.tmpl> HTML file, we would have something like this:
<html>
<head>
<title>Search Engine</title>
<tmpl_var js-head>
</head>
<body bgcolor="white">
<center>
<p>
Please enter a term to search the ticket database.
<p>
<tmpl_var form-start>
Search by <tmpl_var field-type> for <tmpl_var field-string>
<tmpl_var form-submit>
<p>
Status: <tmpl_var field-status>
<p>
Category: <tmpl_var field-category>
<p>
</form>
</body>
</html>
That's all you need for a sticky search form with the above HTML layout.
Notice that you can change the HTML layout as much as you want without
having to touch your CGI code.
=head2 Ex4: user_info.cgi
This script grabs the user's information out of a database and lets
them update it dynamically. The DBI information is provided as an
example, your mileage may vary:
#!/usr/bin/perl
use strict;
use CGI::FormBuilder;
use DBI;
use DBD::Oracle
my $dbh = DBI->connect('dbi:Oracle:db', 'user', 'pass');
# We create a new form. Note we've specified very little,
# since we're getting all our values from our database.
my $form = CGI::FormBuilder->new(
fields => [qw(username password confirm_password
first_name last_name email)]
);
# Now get the value of the username from our app
my $user = $form->cgi_param('user');
my $sth = $dbh->prepare("select * from user_info where user = '$user'");
$sth->execute;
my $default_hashref = $sth->fetchrow_hashref;
( run in 1.551 second using v1.01-cache-2.11-cpan-63c85eba8c4 )