Catalyst-Controller-FormBuilder
view release on metacpan or search on metacpan
lib/Catalyst/Controller/FormBuilder.pm view on Meta::CPAN
Only Template Toolkit, Mason and HTML::Template are currently supported, but
if your templating system's stash requirements are identical to one of these,
simply choose and define it via the C<template_type> config option. Of course,
make sure you have a View to support the template, since this module does not
render templates.
From within your template, you can reference any of FormBuilder's
methods to manipulate form HTML, JavaScript, and so forth. For example,
you might want exact control over fields, rendering them in a C<< <div> >>
instead of a table. You could do something like this:
<!-- root/src/books/edit.tt -->
<head>
<title>[% formbuilder.title %]</title>
[% formbuilder.jshead %]<!-- javascript -->
</head>
<body>
[% formbuilder.start -%]
<div id="form">
[% FOREACH field IN formbuilder.fields -%]
<p>
<label>
<span [% IF field.required %]class="required"[%END%]>[%field.label%]</span>
</label>
[% field.field %]
[% IF field.invalid -%]
<span class="error">
Missing or invalid entry, please try again.
</span>
[% END %]
</p>
[% END %]
<div id="submit">[% formbuilder.submit %]</div>
<div id="reset">[% formbuilder.reset %]</div>
</div>
</div>
[% formbuilder.end -%]
</body>
In this case, you would B<not> call C<FormBuilder.render>, since that would
only result in a duplicate form (once using the above expansion, and
a second time using FormBuilder's default rendering).
Note that the above form could become a generic C<form.tt> template
which you simply included in all your files, since there is nothing
specific to a given form hardcoded in (that's the idea, after all).
You can also get some ideas based on FormBuilder's native Template Toolkit
support at L<CGI::FormBuilder::Template::TT2>.
=head1 CONFIGURATION
You can set defaults for your forms using Catalyst's config method inside
your controller.
__PACKAGE__->config(
'Controller::FormBuilder' => {
new => {
method => 'post',
# stylesheet => 1,
messages => '/locale/fr_FR/form_messages.txt',
},
form_path =>
File::Spec->catfile( $c->config->{home}, 'root', 'forms' ),
method_name => 'form',
template_type => 'HTML::Template',
stash_name => 'form',
obj_name => 'FormBuilder',
form_suffix => 'fb',
attr_name => 'Form',
source_type => 'CGI::FormBuilder::Source::File',
}
);
=over
=item C<new>
This accepts the exact same options as FormBuilder's C<new()> method
(which is a lot). See L<CGI::FormBuilder> for a full list of options.
=item C<form_path>
The path to configuration files. This should be set to an absolute
path to prevent problems. By default, it is set to:
File::Spec->catfile( $c->config->{home}, 'root', 'forms' )
This can be a colon-separated list of directories if you want to
specify multiple paths (ie, "/templates1:/template2"), or an array
ref (ie, [qw/template1 templates2/]).
=item C<form_suffix>
The suffix that configuration files have. By default, it is C<fb>.
=item C<method_name>
Accessor method name available in your controller. By default, it is
C<formbuilder>.
=item C<template_type>
Defines the Catalyst View that the stash will be prepared for. Possible
values are: HTML::Template, Mason, TT. By default, it is C<TT>.
=item C<stash_name>
Not applicable for HTML::Template view. By default, it is C<formbuilder>.
e.g. $c->stash->{formbuilder} = $formbuilder->prepare.
=item C<obj_name>
Not applicable for HTML::Template view. By default, it is C<FormBuilder>.
e.g. $c->stash->{FormBuilder} = $formbuilder.
=item C<attr_name>
The attribute name. By default, it is C<Form>.
e.g. sub edit : Form { ... }
( run in 0.503 second using v1.01-cache-2.11-cpan-ceb78f64989 )