While writing an elementary CGI based contact form, I started to consider how to create a more generic form. I am looking at CGI::Template module as well as the Template::Toolkit CGI Plugin and CGI usage in the Badger book so that I don't re-invent the wheel but I also came up with this before looking at them and would appreciate your erudite opinions.

A form might carry a number of XHTML text type input tags which might vary in name and id, have an increasing tabindex and other attributes which might be fixed by type.

So I imagined defining field type hashes like these.

my %textvals = ( name => '', id => '', tabindex => '', size => 50, ... );
my %areavals = ( name => '', id => '', tabindex => '', size => 150, ... ); my %popupvals = ( name => '', id => '', mutiple => 1, ... );

etc ... for each field type that might exist in the form.

Then creating a number of field value hashes stating what the name and fieldtype of each field in the form. e.g.

my %name = ( name => 'name', fieldtype => 'textfield', }
my %subject = ( name => 'email', fieldtype => 'textfield', } my %msgbody = ( name => 'msgbody', fieldtype => 'textarea', ); my %aselectlist = ( name => 'aselectlist', fieldtype => 'popup', optvals => [qw/option1 option2 option3 option4/], );

then creating an array of form fields :

my @formfields = ( \%name, \%email, \%aselectlist, \%msgbody );

which could be used in this way :

my $page = CGI->new(); my $tb = 0; foreach $field (@formfields) { my $vals; $vals = \%textvals if ($field->fieldtype eq 'textfield') ;
$vals = \%areavals if ($field->fieldtype eq 'textarea') ; $vals = \%popupvals if ($field->fieldtype eq 'popup'); ... etc ... for each field type that might exist in the form. $tb++; $textvals{name} = $field->{name}; $textvals{tabindex} = $tb; $textvals(id} = $field->{name}; .... $page->textfield($vals) if ($field->fieldtype eq 'textfield') ; $page->textarea($vals) if ($field->fieldtype eq 'textarea') ;
$page->popup_menu($vals) if ($field->fieldtype eq 'popup') ; ... etc ... for each field type that might exist in the form. }

Adding fields of any type to the form is then a matter of defining

This construction could be made more complex by allowing many field type hashes for a particular, field type. It is possible for example, there might be varying widths of textfields in a form and there would have to be an array of field type hashes for the variations.

This is probably not the best solution to generic form creation. In fact it might be something only someone who is completely barking or has too much time on their hands would even ever think of but I did wonder how it could be improved. It might even be a load of nonsense to do things this way. It may be horrifically inefficient. I'd like to know what people's thoughts are on this code and coding style.

One criticism I have is there might be more field type specific activity within the foreach loop e.g using the list of options for the popup_menu field. This requirement would increase with the variation in the field types in the form.

I did also wonder how to auto-detect the fieldtype or is the example in the foreach loop. I don't have the skill to change the field type dependent assignment to $vals with something like :

$vals = \%<insert field type mnemonic here>vals;

and the execution of the correct CGI.pm routine to

$page-><insert field type CGI routine here>($vals);

I would like to know if that is possible.


In reply to Is this attempt at a generic form using CGI really silly/inefficient/useless waste of time? by LesleyB

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.