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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |