[Catalyst] C::C::FormBuilder - Forwarding to another action overwrites data from the calling action

Danny Warren danny at io.com
Tue Feb 13 03:11:13 GMT 2007


(Started a new thread, since your previous response answered my basic 
questions, but I think my problem is something else entirely)

I used your below sample code to cook up something that supports a 
naming scheme for multiple form variables in TT, but I am still having 
issues.  I tracked it down to something in C::C::FormBuilder itself that 
I can't seem to get around, and I was wondering if you had any ideas.

It looks to me that forwarding from one "Form" action to another "Form" 
action causes the FormBuilder data from the first action to be 
overwritten.

I added the following lines to Action::TT to confirm this...

     $c->log->debug( 'ACTION: ' . $c->action );
     $c->log->debug( 'REAL ACTION: ' . ( $self->_attr_params->[0] || 
$self->reverse ) );
     $c->log->debug( 'FORM NAME: ' . $controller->_formbuilder->name );


(I also dumped out the entire FormBuilder object, but I won't post that 
here since it is huge)

Here is a snippet from the debug log that illustrates this (in my sample 
code "foo" forwards to "foo_search"):

   [debug] Form (foo): Looking for config file foo.fb
   [debug] Form (foo): Found form config 
/home/dwarren/MyApp/root/forms/foo.fb

   [debug] Form (foo_search): Looking for config file foo_search.fb
   [debug] Form (foo_search): Found form config 
/home/dwarren/MyApp/foo_search.fb

   [debug] ACTION: foo
   [debug] REAL ACTION: foo_search
   [debug] FORM NAME: foo_search

   [debug] ACTION: foo
   [debug] REAL ACTION: foo
   [debug] FORM NAME: foo_search

Can you confirm this, or am I doing something wrong here?  I am still 
somewhat green with Catalyst, so I might be misunderstanding exactly how 
forwards work.  From reading the docs, I was under the impression that 
two actions should not clobber each other upon forwarding (since 
forwarded actions are processed inside an eval).

Thanks again!
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