http://www.perlmonks.org?node_id=239218

...meaning Yet Another HTML Module. I've come up with a module called FormManager.pm that I've been using quite a bit in recent projects. I'd like the opinions of the fine monks around here as to whether it's worth turning into a CPAN module, or if it's the hacked-up, cobbled-together redundant version of something else as is often the case. For me, it's shaved off quite a bit of development time. Here's an example
use FormManager; my $query = new CGI(); my $fm = new FormManager (layout => [new FormManager::Text (name => 'phone_number', heading => 'Enter +your phone number', validate => 'phone'), new FormManager::Select (name => 'likes_ice_cream', heading => 'Do + you like ice cream?', option_list => ['Yes', 'No']), new FormManager::Hidden (name => 'action', value => 'submit_this_f +orm') ], form_submit => 'Submit this form' ); print "Content-Type: text/html\n\n"; # This script can be called in two ways # 1) Initially to get the form # 2) By submitting the form # If this is case 1) if ($query->param ('action') ne 'submit_this_form') { print $fm->getHtml(); } else { # Ensure that the input is valid. If not, re-print the form if (!$fm->processQuery()) { print $fm->getHtml(); } else { print "Your phone number is : " . $fm->getValue ('phone_number')" +. " and you " . ($fm->getValue ('likes_ice_cream') eq 'No' ? "don't l +ike ice cream." : "like ice cream."); } }
There are numerous features this provides over pure CGI.pm.

1) It's easily extensible. For instance, one could create a FormManager::Date component which consists of three select boxes for month/day/year. In your code, you could treat this as a single input.

2) Validation. This module can validate in a number of ways. So you can use the premade options like 'phone', 'integer', etc, or create your own. Select boxes are automatically checked to ensure the value matches one of the options presented. Validation of all form components is done with a single call to processQuery($query).

3) Easy user-friendliness. If someone enters an incorrect value, or leaves out required information, simply call $fm->getHtml() again after validation. FormManager will fill in all of the old values so that the user doesn't have to re-enter an entire form because of one mistake.

Thanks in advance for the perl-wisdom of the ages.
James