CGI::FormBuilder is a cool if sprawling module to automatically build HTML forms with sticky widgets, JS & Perl validation.

I love the idea, but the interface is a bit too dictatorial for my liking. But while I wait for a more modular, pluggable version, here's is a horrible little hack to gather the bits that would normally get passed to TT2.

package AHP::FakeTemplate; =head1 DESCRIPTION Tames CGI::FormBuilder. The class pretends to be a Template in order to catch the CGI::FormBuilder vars passed to process so that we can use them outside of the callback. =head1 SYNPOSIS my $fb_vars; my $form = CGI::FormBuilder->new( ... template => { engine => AHP::FakeTemplate->new_fake(\$fb_vars), type => 'TT2', template => 'ignored', }, ); $form->render(); ... $template->process('my.tt2', { form => $fb_vars, ... }); my.tt2: [% form.start %] [% FOREACH f = form.fields %] [%# "<pre>"; Dumper.dump(f) | html ; "</pre>" %] [% f.required ? "<b>$f.label</b>" : f.label %] [% f.field %] [% f.comment %]<br> [% IF f.invalid %] Missing or invalid entry. [% END %] <br> [% END %] [% form.submit %] <br> [% form.end %] =cut use Carp (); use Template (); @ISA = ('Template'); # It's not really, but C::FB checks # I'd prefer we couldn't act like one use strict; sub new { Carp::croak "$_[0] is not really a Template"; } sub new_fake { my ($class, $target_ref) = @_; return bless $target_ref, $class; } # This sets the reference passed into new_fake sub process { my ($target_ref, $tmpl_ignored, $tt2vars, $tt2output_ref) = @_; $$target_ref = $tt2vars; $$tt2output_ref = ''; return 1; } 1;

In reply to Taming CGI::FormBuilder (Hack) by bsb

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.