[html-formfu] Re: H::F::Model::DBIC and adding multiple empty rows

Lance A. Brown lance at bearcircle.net
Mon Apr 20 16:21:14 GMT 2009


Carl Franks wrote:
> If it doesn't seem to work - dump $form->params(), so you can see
> which params are being accepted.

$form->params() looks correct at all stages.

Here is an example:

I have a 'Paper' with two 'Authors': Joe Shmo and John Doe.  I edit the
paper, the form displays both authors and an empty author line.  I click
my 'Add Author' button to add one more Author Line, then enter 'New
Author 1' and 'New Author 2' into the empty Author linues and click 'submit:

NOTE: Each Author has a 'rank' field that lets me store the proper
ordering for the authors, which I update in my controller to set the
correct order.

It seems like somewhere along the line Model::DBIC is using the existing
has_many data instead of looking at the submitted form data to determine
how many 'has_many' items there are to add/update.

The variables submitted to Catalyst look like:

[debug] Body Parameters are:
.-------------------------------------+---------------------------------.
| Parameter                           | Value
     |
+-------------------------------------+---------------------------------+
| abstract                            | abstract

| author_count                        | 4

| authors.author_id_1                 | 1606

| authors.author_id_2                 | 1649

| authors.author_id_3                 |

| authors.author_id_4                 |

| authors.name_1                      | Joe Shmo

| authors.name_2                      | John Doe

| authors.name_3                      | New Author 1

| authors.name_4                      | New Author 2

| authors.rank_1                      | 1

| authors.rank_2                      | 2

| authors.rank_3                      |

| authors.rank_4                      |

| bibtex_citation                     |

| date                                | some day

| institution                         | no where

| keywords                            |

| paper_id                            | 645

| submit                              | Submit

| title                               | fun with create_related

'-------------------------------------+--------------------------------------'

The DBIC_TRACE=1 for this submission follows.
The 'count is' and 'repeating X' lines are 'warn()' calls I added to
Repeatable so I could see what is going on.
The 'max is' is the max set in H::F::Model::DBIC _save_has_many and
controls the number of repetitions that sub goes through.

count is 4 at
/usr/lib/perl5/site_perl/5.8.5/HTML/FormFu/Element/Repeatable.pm line 148.
repeating 4 at
/usr/lib/perl5/site_perl/5.8.5/HTML/FormFu/Element/Repeatable.pm line 152.
SELECT me.user_id, me.dssid, me.netid, me.name, me.uid, me.gid, me.type,
me.state FROM dss.users me WHERE ( user_id = ? ): '192'
SELECT me.paper_id, me.year, me.seqnum, me.title, me.institution,
me.date, me.abstract, me.keywords, me.bibtex, me.style, me.old_paperid,
me.link FROM papers me WHERE ( me.paper_id = ? ): '645'
SELECT me.author_id, me.paper_id, me.name, me.rank FROM authors me WHERE
( me.paper_id = ? ) ORDER BY rank ASC: '645'
SELECT me.paper_id, me.year, me.seqnum, me.title, me.institution,
me.date, me.abstract, me.keywords, me.bibtex, me.style, me.old_paperid,
me.link FROM papers me WHERE ( me.paper_id = ? ): '645'
max is 2 at /usr/lib/perl5/site_perl/5.8.5/HTML/FormFu/Model/DBIC.pm
line 474.
SELECT me.author_id, me.paper_id, me.name, me.rank FROM authors me WHERE
( ( me.author_id = ? AND me.paper_id = ? ) ) ORDER BY rank ASC: '1606',
'645'
SELECT me.author_id, me.paper_id, me.name, me.rank FROM authors me WHERE
( ( me.author_id = ? AND me.paper_id = ? ) ) ORDER BY rank ASC: '1649',
'645'
INSERT INTO authors ( name, paper_id, rank) VALUES ( ?, ?, ? ): 'New
Author 1', '645', '3'

I dump the form params in three places in my controller code since I'm
mucking with it.  First, here is the relevant sub:

sub edit :Path('/edit') :Args(1) :FormConfig('paper.yml')
{
    my ($self, $c, $paper_id ) = @_;

    # Plug in our CSS
    $c->assets->include('assets/css/form.css');

    # Plug in Jquery autocomplete
    $c->forward('autocomplete');

    my $paper = $c->model('Papers::Papers')->find($paper_id);

    my $form = $c->stash->{form};

    $form->model->default_values($paper);

    $c->log->debug('After default values');
    $c->log->debug(Dumper($form->params));

    if ($form->submitted)
    {
        $c->log->debug('in submitted');
        $c->log->debug(Dumper($form->params));

        ## Force rank variables to be set to what I want
        my $count = $form->param_value('author_count');
        foreach my $c (1 .. $count)
        {
            $form->add_valid("authors.rank_$c", $c);
        }
    }

    if ($form->submitted_and_valid)
    {
        $c->log->debug('in submitted and valid');
        $c->log->debug(Dumper($form->params));
        $form->model->update($p);
    }

    $c->stash->{template} = "edit_paper.tt2";
}

And here is the $form->params dump output:

[debug] After default values
[debug] $VAR1 = {
          'author_count' => '4',
          'authors' => {
                       'name_3' => 'New Author 1',
                       'rank_3' => '',
                       'name_1' => 'Joe Shmo',
                       'author_id_1' => '1606',
                       'rank_2' => '2',
                       'author_id_3' => '',
                       'name_4' => 'New Author 2',
                       'rank_4' => '',
                       'rank_1' => '1',
                       'author_id_4' => '',
                       'author_id_2' => '1649',
                       'name_2' => 'John Doe'
                     },
          'date' => 'some day',
          'submit' => 'Submit',
          'keywords' => '',
          'bibtex_citation' => '',
          'abstract' => 'abstract',
          'title' => 'fun with create_related',
          'paper_id' => '645',
          'institution' => 'no where'
        };
[debug] in submitted
[debug] $VAR1 = {
          'author_count' => '4',
          'authors' => {
                       'name_3' => 'New Author 1',
                       'rank_3' => '',
                       'name_1' => 'Joe Shmo',
                       'author_id_1' => '1606',
                       'rank_2' => '2',
                       'author_id_3' => '',
                       'name_4' => 'New Author 2',
                       'rank_4' => '',
                       'rank_1' => '1',
                       'author_id_4' => '',
                       'author_id_2' => '1649',
                       'name_2' => 'John Doe'
                     },
          'date' => 'some day',
          'submit' => 'Submit',
          'keywords' => '',
          'bibtex_citation' => '',
          'abstract' => 'abstract',
          'title' => 'fun with create_related',
          'paper_id' => '645',
          'institution' => 'no where'
        };
[debug] in submitted and valid
[debug] $VAR1 = {
          'author_count' => '4',
          'authors' => {
                       'name_3' => 'New Author 1',
                       'rank_3' => 3,
                       'name_1' => 'Joe Shmo',
                       'author_id_1' => '1606',
                       'rank_2' => 2,
                       'author_id_3' => '',
                       'name_4' => 'New Author 2',
                       'rank_4' => 4,
                       'rank_1' => 1,
                       'author_id_4' => '',
                       'author_id_2' => '1649',
                       'name_2' => 'John Doe'
                     },
          'date' => 'some day',
          'submit' => 'Submit',
          'keywords' => '',
          'bibtex_citation' => '',
          'abstract' => 'abstract',
          'title' => 'fun with create_related',
          'paper_id' => '645',
          'institution' => 'no where'
        };


-- 
 GPG Fingerprint: 409B A409 A38D 92BF 15D9 6EEE 9A82 F2AC 69AC 07B9
 CACert.org Assurer



More information about the HTML-FormFu mailing list