[Catalyst] More than one form per page with C::C::FormBuilder?

Danny Warren danny at io.com
Mon Feb 12 21:49:09 GMT 2007


Thank you Juan!  This is along the lines of what I am looking for, and 
gives me a good place to start from.  I will play around with this today 
and get back to you (unfortunately dev.catalyst.perl.org is unresponsive 
at the moment, but I did get a chance to browse the repo and see the 
current C::C::FormBuilder source before it went down).

Once I get something working I will post it here, and I would love to 
help come up with something reusable for CPAN to solve this if the code 
is general enough.

Thanks again for your help and being so active on the list.  Reading 
through you other posts in the archive has been quite helpful.

Danny

Juan Camacho 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;
> }
> 
> _______________________________________________
> List: Catalyst at lists.rawmode.org
> Listinfo: http://lists.rawmode.org/mailman/listinfo/catalyst
> Searchable archive: http://www.mail-archive.com/catalyst@lists.rawmode.org/
> Dev site: http://dev.catalyst.perl.org/
> 



More information about the Catalyst mailing list