[Catalyst] More than one form per page with C::C::FormBuilder?
Juan Camacho
jc5826 at gmail.com
Mon Feb 12 13:22:29 GMT 2007
On 2/12/07, Juan Camacho <jc5826 at gmail.com> wrote:
> On 2/11/07, Danny Warren <danny at io.com> wrote:
> > Hey all!
> >
> > I am trying to figure out the most elegant way to build/include multiple
> > forms in one page using Catalyst::Controller::FormBuilder.
>
> I'm in a bit of rush right now and won't have time to respond today,
> so I'm pasting a response I had previously sent to someone else.
>
> Try setting up your own Action class. This is code I had provided
> someone else trying to do something similar. You probably don't want
> the extra _add_form_to method -- I was simply parroting back some code
> I was provided. The general idea is to provide a top level hash key
> for each form. I don't necessarily recommend the key name used in
> this example -- a better one would be the $formbuilder->name. It
> would be a good idea to provide a more generalized approach that can
> be released to CPAN as C-C-FormBuilder-MultiForm module. Volunteers
> welcome :) Let me know if this helps.
>
> package MyApp::Controller::Base;
>
> use strict;
> use base 'Catalyst::Controller::FormBuilder';
>
> __PACKAGE__->config(
> 'Controller::FormBuilder' => {
> action => 'MyApp::Action::FormBuilder',
> }
> );
>
> package MyApp::Controller::Company;
>
> use base qw/MyApp::Controller::Base/;
>
> sub edit : Local Form {
> my ($self, $c) = @_;
>
> $c->stash->{template} = 'books/edit.tt2';
>
> my $form = $self->formbuilder;
>
> if ($form->submitted && $form->validate) {
> return $c->forward('/company/save');
> }
>
> $c->forward('/company/searchform');
> }
>
> sub searchform : Local Form('/searchform') {
> my ($self, $c) = @_;
> my $searchform = $self->formbuilder;
> my $fields = $searchform->fields;
> $c->stash->{template} = 'searchresults.tt2';
> $c->stash->{search_results} = $c->model('Listings')->search($fields);
> }
>
> package MyApp::Action::FormBuilder;
>
> use strict;
> use base 'Catalyst::Controller::FormBuilder::Action::TT';
>
> sub setup_template_vars {
> my $self = shift;
> my ( $controller, $c ) = @_;
>
> $self->SUPER::setup_template_vars(@_);
>
> my $forms = {};
> my $name = $self->_attr_params->[0] || $self->reverse;
> _add_form_to( $forms, split('/', $name), $c->stash->{FormBuilder} );
>
> $c->stash->{forms} = $forms;
> }
>
> sub _add_form_to {
> my $href = shift;
> my $node_count = scalar @_;
> return if !$node_count;
> my $node_count = scalar @_;
> return if !$node_count;
> return shift if $node_count == 1;
> my $node = shift;
> $href->{$node} = _add_form_to( { }, @_ );
> return $href;
> }
>
btw, this only work off C-C-FormBuilder that's up on
http://dev.catalyst.perl.org/repos/Catalyst/trunk/Catalyst-Controller-FormBuilder/
It will eventually be released.
More information about the Catalyst
mailing list