[html-formfu] Render just a single element?

Benjamin Martin benmartin at venda.com
Thu Jul 12 15:27:26 GMT 2012


On 12/07/12 16:01, Robyn Jonahs wrote:
> Is there a tutorial or documentation somewhere on this?

There is a fair amount of FormFu information out there on the interwebs, 
I just did a google for "formfu catalyst" and found many resources, such as:

http://search.cpan.org/~hkclark/Catalyst-Manual-5.9002/lib/Catalyst/Manual/Tutorial/09_AdvancedCRUD/09_FormFu.pod

If you are starting out, I would suggest trying things out in the most 
simple way (no catalyst) to figure out to how formfu and TT work 
together, then apply that to your Catalyst app, in a nice clean way 
(like the scratch code below)

This way you might avoid design mistakes and the dreaded fat controller 
code (been there, done that)

Once you're familar with TT and FF, see what controller base classes 
from cpan can bring to the party :)

--- snip ---

use strict;
use warnings;
use HTML::FormFu;
use Template;

# instead of a YAML file... just pass in the data structure..
my $formfu_cfg = {
     action          => '/',
     method          => 'POST',
     auto_fieldset   => 1,
     auto_label      => 1,
     elements        => [
         {
             type    => 'Text',
             name    => 'box1',
             label   => 'Text box',
             constraints => ['Integer'],
         },
         {
             type    => 'Select',
             name    => 'sel1',
             options => [ [ 'm', 'Male' ],  [ 'f', 'Female' ] ]
         },
     ],
     constraints => ['SingleValue'],
};
my $form     = HTML::FormFu->new( $formfu_cfg );

# read in template source from __DATA__ section
my $template_src = join('', <DATA>);

# construct data that represents a form submission
my $form_submission = { box1 => 'test data A', sel1 => 'm' };

my $template = Template->new();

# process all the bits..

$form->process( $form_submission );

if ( $form->submitted_and_valid ) {

     # form ok :)
     print "Yhay!\n";
}
else {
     # display the form
     my $template_out;
     my $template_vars = { form => $form };
     $template->process(\$template_src, $template_vars, \$template_out);

     print $template_out;
}


__DATA__

<h1> Hello </h1>

[% form %]



More information about the HTML-FormFu mailing list