[html-formfu] Creating & Editing Records in a Related Table

Scott Simpson scott at simpzoid.com
Tue Sep 25 11:32:44 GMT 2012



-----Original Message-----
From: Carl Franks [mailto:fireartist at gmail.com] 
Sent: 25 September 2012 12:10
To: Scott Simpson
Subject: Re: [html-formfu] Creating & Editing Records in a Related Table

On 25 September 2012 11:57, Scott Simpson <scott at simpzoid.com> wrote:
>
>
> -----Original Message-----
> From: Carl Franks [mailto:fireartist at gmail.com]
> Sent: 25 September 2012 10:20
> To: HTML Form Creation, Rendering and Validation Framework
> Subject: Re: [html-formfu] Creating & Editing Records in a Related 
> Table
>
> On 24 September 2012 15:06,  <scott at simpzoid.com> wrote:
>>> On 24 September 2012 11:22,  <scott at simpzoid.com> wrote:
>>>> Dear All,
>>>>
>>>> I have been the following 2 methods in a controller:
>>>>
>>>> sub create :Chained('base') :PathPart('create')
>>>> :FormConfig('author/edit.conf') {
>>>>         my ( $self, $c ) = @_;
>>>>
>>>>         my $form = $c->stash->{form};
>>>>         $form->stash( schema => $c->stash->{schema} );
>>>>
>>>>         if ( $form->submitted_and_valid ) {
>>>>
>>>>              my $author =
>>>> $form->stash->{schema}->resultset('Author')->new_result({});
>>>>              $form->model->update($author);
>>>>              $c->response->redirect( $c->uri_for(
>>>> $self->action_for('list')) );
>>>>         }
>>>>         $c->stash(template => 'author/formfu_create.tt2'); }
>>>>
>>>> sub edit :Chained('object') :PathPart('edit') :Args(0)
>>>>         :FormConfig('author/edit.conf') {
>>>>         my ( $self, $c, $id ) = @_;
>>>>
>>>>         my $form = $c->stash->{form};
>>>>
>>>>         if ( $form->submitted_and_valid ) {
>>>>                 $form->model->update($c->stash->{rs});
>>>>                 $c->response->redirect( $c->uri_for(
>>>> $self->action_for('list')) );
>>>>         } else {
>>>>                 $form->model->default_values($c->stash->{rs});
>>>>         }
>>>>         $c->stash(template => 'author/formfu_create.tt2'); }
>>>>
>>>> The methods give the following results:
>>>>
>>>> Edit gives
>>>> SELECT me.id, me.name FROM author me WHERE ( me.id = ? ): '1'
>>>> UPDATE author SET name = ? WHERE ( id = ? ): 'scott', '1'
>>>> SELECT me.id, me.address, me.author_id FROM address me WHERE ( ( 
>>>> me.id = ?
>>>> AND me.author_id = ? ) ): '1', '1'
>>>> UPDATE address SET address = ? WHERE ( id = ? ): '13 thriplee road',
'1'
>>>>
>>>> Create Gives
>>>> INSERT INTO author ( name) VALUES ( ? ): 'Marge'
>>>>
>>>> ...and finally the question.  Why does the create method not do an 
>>>> insert into the address table and what do I have have to do to 
>>>> persuade the create method to do said insert?
>>>
>>> I haven't checked the Model::DBIC code, but I suspect it's because 
>>> the author row doesn't exist at the time it would be creating the 
>>> related rows.
>>>
>>> Try adding
>>>     $author->insert;
>>> before your
>>>     $form->model->update($author);
>>>
>>> Carl
>>>
>>> _______________________________________________
>>> HTML-FormFu mailing list
>>> HTML-FormFu at lists.scsys.co.uk
>>> http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/html-formfu
>>>
>> This time create gives:
>>
>> INSERT INTO author () VALUES ():
>> UPDATE author SET name = ? WHERE ( id = ? ): 'simp', '15'
>>
>> Still nothing for the address table
>
> I'm a bit stumped by this - I've just checked an app I'm working on 
> just now, and it does exactly what you're wanting it to do.
> I checked the code for create(), and it effectively just does: $row= 
> new_result({}); update($row); - which is what you're doing manually.
>
> Can you send your form config and DBIC resultset class files for 
> Author and Address?
> Carl
>
>
> Carl,
>
> Thanks very much for your efforts.  Files attached.

Hi Scott,

You need to add this to your Repeatable element:

model_config:
    empty_rows: 1
    new_rows_max: 1

I've used YAML, in case the Config::General tags get hidden in your email
viewer, but I'm sure you'll be able to translate them.
'empty_rows' displays the fields to add a new related row, so isn't strictly
necessary, depending on how you're rendering your form.
'new_rows_max' is the important setting, and will default to the value of
'empty_rows' if it's missing.

Carl

Carl,

Having struggled with this for weeks I am utterly delighted to now have a
working prototype.  Thanks.

Regards,
Scott




More information about the HTML-FormFu mailing list