|
NAMEHTML::SuperForm - HTML form generatorSYNOPSISuse HTML::SuperForm; use Apache::Constants qw(OK); sub handler { my $r = shift; my $form = HTML::SuperForm->new($r); my $text = $form->text(name => 'text', default => 'Default Text'); my $textarea = $form->textarea(name => 'textarea', default => 'More Default Text'); my $select = $form->select(name => 'select', default => 2, values => [ 0, 1, 2, 3], labels => { 0 => 'Zero', 1 => 'One', 2 => 'Two', 3 => 'Three' }); my $output = <<"END_HTML"; <html> <body> <form> Text Field: $text<br> Text Area: $textarea<br> Select: $select </form> </body> </html> END_HTML $r->content_type('text/html'); $r->send_http_header; $r->print($output); return OK; } OR #!/usr/bin/perl my $form = HTML::SuperForm->new(); my $text = $form->text(name => 'text', default => 'Default Text'); my $textarea = $form->textarea(name => 'textarea', default => 'More Default Text'); my $select = $form->select(name => 'select', default => 2, values => [ 0, 1, 2, 3], labels => { 0 => 'Zero', 1 => 'One', 2 => 'Two', 3 => 'Three' }); my $output = <<"END_HTML"; <html> <body> <form> Text Field: $text<br> Text Area: $textarea<br> Select: $select </form> </body> </html> END_HTML print "Content-Type: text/html\n\n"; print $output; DESCRIPTIONUsed in its basic form, this module provides an interface for generating basic HTML form elements much like HTML::StickyForms does. The main difference is HTML::SuperForm returns HTML::SuperForm::Field objects rather than plain HTML. This allows for more flexibilty when generating forms for a complex application.To get the most out of this module, use it as a base (Super) class for your own form object which generates your own custom fields. If you don't use it this way, I guess there's really nothing Super about it. Example are shown later in the document. The interface was designed with mod_perl and the Template Toolkit in mind, but it works equally well in any cgi environment. METHODSCONSTRUCTOR
ACCESSORS AND MUTATORSThese methods get and set values contained in the form object.
Any other attribute you specify can be accessed by calling its method (i.e. if you pass in ( target => 'newWindow', onSubmit => 'CheckForm()' ), $form->target will return 'newWindow', and $form->onSubmit will return 'CheckForm()'). The names are case sensitive so be consistent.
GET AND SETSince I intended HTML::SuperForm's main use to be as a base class, I made these methods so subclasses can store information within the object without worrying about overriding important keys in the object.
INTERNAL METHODSYou probably won't ever need to use these methods unless you are in a subclass of HTML::SuperForm or HTML::SuperForm::Field.
FIELD METHODSThese methods return objects with their string operators overloaded to output HTML.
EXAMPLESThis example shows how to make a form object that can generate a counter field along with all the other basic fields. A counter field consists of a text field, an increment button and a decrement button. Consult the documentation for HTML::SuperForm::Field for more information about field inheritance.package myForm; use strict; use myForm::Counter; use base 'HTML::SuperForm'; sub counter { return myForm::Counter->new(@_); } sub javascript { my $js = <<END_JAVASCRIPT; <script language="JavaScript"> function Increment(field) { field.value++; } function Decrement(field) { field.value--; } </script> END_JAVASCRIPT return $js; } 1; package myForm::Counter; use strict; use base 'HTML::SuperForm::Field'; use HTML::SuperForm::Field::Text; sub prepare { my $self = shift; my $form_name = $self->form->name; my $field_name = $self->name; my $js_name = "document.$form_name.$field_name"; my $text = HTML::SuperForm::Field::Text->new(name => $self->name, default => $self->value, size => 4); $self->set(text => $text); $self->set(inc => qq|<a style="cursor: pointer" onmouseup="Increment($js_name)"><img src="/icons/up.gif" border=0></a>|); $self->set(dec => qq|<a style="cursor: pointer" onmouseup="Decrement($js_name)"><img src="/icons/down.gif" border=0></a>|); } sub inc { my $self = shift; return $self->get('inc'); } sub dec { my $self = shift; return $self->get('dec'); } sub text { my $self = shift; return $self->get('text'); } sub arrows_right { my $self = shift; my ($text, $inc, $dec) = $self->get('text', 'inc', 'dec'); my $tag = "<table>\n"; $tag .= qq| <tr>\n|; $tag .= qq| <td align="center">$text</td>\n|; $tag .= qq| <td align="center">$inc<br/>$dec</td>\n|; $tag .= qq| </tr>\n|; $tag .= "</table>\n"; return $tag; } sub arrows_left { my $self = shift; my ($text, $inc, $dec) = $self->get('text', 'inc', 'dec'); my $tag = "<table>\n"; $tag .= qq| <tr>\n|; $tag .= qq| <td align="center">$inc<br/>$dec</td>\n|; $tag .= qq| <td align="center">$text</td>\n|; $tag .= qq| </tr>\n|; $tag .= "</table>\n"; return $tag; } sub default_layout { my $self = shift; my ($text, $inc, $dec) = $self->get('text', 'inc', 'dec'); my $tag = "<table>\n"; $tag .= qq| <tr><td align="center">$inc</td></tr>\n|; $tag .= qq| <tr><td align="center">$text</td></tr>\n|; $tag .= qq| <tr><td align="center">$dec</td></tr>\n|; $tag .= "</table>\n"; return $tag; } sub to_html { my $self = shift; my $tag = $self->default_layout; return $tag; } 1; This might seem complex but by using it this way you get the following functionality: package myHandler; use strict; use myForm; use Apache::Constants qw(OK); use Template; sub handler($$) { my $self = shift; my $r = shift; my $form = myForm->new($r); my $tt = Template->new(INCLUDE_PATH => '/my/template/path'); my $output; $tt->process('my_template.tt', { form => $form }, \$output); $r->content_type('text/html'); $r->send_http_header(); $r->print($output); return OK; } 1; my_template.tt: <html> <head> <title>Flexibility with HTML::SuperForm</title> [% form.javascript %] </head> <body> [% form.start_form %] Default Counter Layout: [% form.counter(name => 'counter1', default => 0) %] <br/> Counter with increment/decrement buttons on the left: [% form.counter(name => 'counter2', default => 0).arrows_left %] <br/> Counter with increment/decrement buttons on the right: [% form.counter(name => 'counter3', default => 0).arrows_right %] <br/> Counter with multiple increment/decrement buttons wherever you want: <br/> [% counter = form.counter(name => 'counter4', default => 0) %] <table> <tr><td>[% counter.inc %]</td><td></td><td>[% counter.inc %]</tr> <tr><td></td><td></td>[% counter.text %]<td></tr> <tr><td>[% counter.dec %]</td><td></td><td>[% counter.dec %]</tr> </table> [% form.submit %] [% form.end_form %] </body> </html> SEE ALSOHTML::SuperForm::Field, HTML::SuperForm::Field::Text, HTML::SuperForm::Field::Textarea, HTML::SuperForm::Field::Select, HTML::SuperForm::Field::Checkbox, HTML::SuperForm::Field::Radio, HTML::SuperForm::Field::CheckboxGroup, HTML::SuperForm::Field::RadioGroup TODO Document its usage for fully. Give more examples. AUTHORJohn Allwine <jallwine86@yahoo.com>COPYRIGHTThis program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.The full text of the license can be found in the LICENSE file included with this module.
Visit the GSP FreeBSD Man Page Interface. |