[Catalyst] Odd Formbuilder behavior - parsing twice?

Fahd subscriber at sharova.net
Wed May 6 05:07:58 GMT 2009


Hello,

I'm writing a Catalyst app based on the examples in the Catalyst book
(basic CRUD) and am experiencing odd behavior with FormBuilder.
I currently have a simple model with projects and clients.
I am forwarding /project/add to /project/edit. 
When I directly edit a project I see formbuilder load the edit.fb once. 
When I try to add a new project, I see formbuilder load the edit.fb file
twice. In 'edit' I seem to be able to dynamically set the form
properties (label for name) and create a select element from database
values.  'project/add' seems to build the form right the first time, but
seems to use defaults (edit.frb and no database values in select) the
second time. I've deduced this from the very verbose FormBuilder debug
output - it shows the same 'generated tag' three time for add, only once
(and with correct values) for edit.

thanks,

fahd

_ project/add:

[debug] "GET" request for "project/add" from "127.0.1.1"
[debug] Path is "project/add"
[debug] Form (project/edit): Looking for config file project/edit.fb
[debug] Form (project/edit): Found form config
/home/sultan/Workspace/TnTr/root/forms/project/edit.fb
[debug] Form (project/edit): Looking for config file project/edit.fb
[debug] Form (project/edit): Found form config
/home/sultan/Workspace/TnTr/root/forms/project/edit.fb
[debug] Rendering template "project/edit.tt2"

_ project/edit:

[debug] "GET" request for "project/edit/1" from "127.0.1.1"
[debug] Path is "project/edit"
[debug] Arguments are "1"
[debug] Form (project/edit): Looking for config file project/edit.fb
[debug] Form (project/edit): Found form config
/home/sultan/Workspace/TnTr/root/forms/project/edit.fb
[debug] Rendering template "project/edit.tt2"

_ root/forms/project/edit.fb :

name: project_edit
method: post
fields:
  name:
    label: Project Name
    type: text
    size: 30
    required: 1
  description:
    label: Project Description
    type: text
    size: 30
    required: 1
  client:
   label: Client
   type: select
   required: 1

_ TnTr::Controller::Project:

sub add : Local Form('/project/edit') {
    my ($self, $c) = @_;
    $c->stash->{template} = 'project/edit.tt2';
    $c->forward('edit', []);
}
sub edit : Local Form {
    my ($self, $c, $id) = @_;

    my $form = $self->formbuilder;
    my $project = $c->model('TnTrDB::Projects')->
                                find_or_new({id => $id});
    $form->field(name => 'client',
                 options => [ map { [$_->id,$_->name] }
                            $c->model('TnTrDB::Clients')->all ],
                 );
    $form->field(name => 'name',
                label => 'Bobo',
                );
    if ($form->submitted && $form->validate) {
        $project->name($form->field('name'));
        $project->client($form->field('client'));

        $project->update_or_insert;

        $c->stash->{message} =
          ($id > 0 ? 'Updated ' : 'Added ') . $project->name;
        $c->forward('list');
    }
    else {
       if(!$id){
             $c->stash->{message} = 'Adding a new Project';
    $form->field(name => 'client',
                 options => [ map { [$_->id,$_->name] }
                            $c->model('TnTrDB::Clients')->all ],
                 );
        }
        else {
            # only set values if exists in database
            # if I don't do this catalyst blows up on add
            $form->field(name => 'name',
                                value => $project->name);
            $form->field(name => 'client',
                                value => $project->client->id);
        }
    }
}




More information about the Catalyst mailing list