[Catalyst] Flow control with forms

Daniel Hembree danny at dynamical.org
Sun Jan 20 19:27:24 GMT 2008


I'm  having difficulty using a foreach loop to read through a file and
call a form to  edit the data read and put it in the data base. The for
loop executes but the form is only called on the last data item. I'm
sure there's something I'm missing regarding flow control. I've tried it
two different ways, with the form inline in the for loop:

   my $form=$self->formbuilder;
          $form->field(
                       name => 'Category',
                       options => [map { [$_->id, $_->label]}
                                   $c->model('ToolbarDB::cattab')->all ]);
#get domain from file
...
#valid domain name
              if($ipstr)
                {
                  $c->stash->{Dom}=$domain;
                  $form->field->{'Domain'}=$domain;
                  if ($form->submitted)
                    {
                      $c->stash->{domcat} = $form->field->{Category};
                      $url=$c->model('toolbarDB::URL' )->find_or_new({
site_url => $urlstr, site_ip => $ipstr});
                      if($url && $url->in_storage)
                        {
                          my
$sr=$c->model('ToolbarDB::siterecord')->find( { site_id => $url->rec_id });
                          if($sr && $sr->in_storage)
                            {
                              $sr->{category}=$c->stash->{domcat};
                            }
                          next;
                        }
                      elsif($url)
....
                    }# end if submitted
                  $c->stash->{template}='insertalexa/insertTopSites.tt2';

or calling the form from another local subroutine in the same package.
...
in the subroutine with the for loop:
              if($ipstr)
                {
                  $c->stash->{Dom}=$domain;
                  $c->forward('updcat',[$domain]);
...
and updcat:

sub updcat : Local Form
    {
    my ($self,$c, $dom) = @_;
    $DB::single=1;
    $c->stash->{template}='insertalexa/insertTopSites.tt2';
    my $form=$self->formbuilder;
    $form->field->{'Domain'}=$dom;
    $form->field(
                 name => 'Category',
                 options => [map { [$_->id, $_->label]}
                             $c->model('ToolbarDB::cattab')->all ]);
    if ($form->submitted && $form->validate)
      {
        $c->stash->{domcat} = $form->field->{Category};
        return;
      }
  }

in both cases, the form renders correctly but only after the last item
in the loop has been read in. The other items visit the form, but it
doesn't render or wait for a submit. I suspect I'm missing something
about catalyst flow control, I've read through all the docs and Jonathan
Rockway's new Catalyst book. The form itself is copied from the book:

           <head>
             <title>[% formbuilder.title %]</title>
             [% formbuilder.jshead %]<!-- javascript -->
           </head>
            <body>
                <H3> Domain is [% Dom %] </H3>
             [% 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 -%]
        <div>
        <IFrame name="viewdom",
                src="http://www.[% Dom %]",
                align="center",
                Width="850",
                Heigth="700,
                scrolling="yes">Domain View</IFrame>
        </div>
           </body>
and defined this way;

title: Domain Category Editor
name: Catgegory Edit
method: post
fields:
Domain:
  label: Domain
  type: text
  size: 128
Submit: Set Category

But I'm not seeing it. Is it even possible to keep calling the form
with new data from a for loop like this? If not, what should I do?








More information about the Catalyst mailing list