[html-formfu] Request advice RE FormFu and join tables

Carl Franks fireartist at gmail.com
Wed Apr 15 14:38:55 GMT 2009


2009/4/14 Dennis Daupert <ddaupert at gmail.com>:
>
>
> On Tue, Apr 14, 2009 at 2:02 PM, Carl Franks <fireartist at gmail.com> wrote:
>>
>> Hi Dennis,
>>
>> If you checkout the test files from the HTML-FormFu-Model-DBIC
>> distribution, I think these ones match the setups you want:
>>
>> t/update/many_to_many_select.t
>> t/update/many_to_many_select.yml
>> t/default_values/has_many_select.t
>> t/default_values/has_many_select.yml
>
> Hi Carl,
>
> Thank you for your kindness. I've had a look at the t/ code. Maybe I'm
> getting closer, hard to know, my head hurts. I still haven't connected the
> dots yet. If you don't mind, can I show you my yml file?
>
> I have two Selects in my config file:
>
> ---
> auto_fieldset:
>   legend: Select Project
> indicator: submit
> elements:
>    - type: Select
>      name: project
>      label: Select Project
>      model_config:
>         resultset: Projects
>         condition:
>            active: 1
>    - type: Select
>      name: directory
>      label: Select Directory
>      multiple: 1
>      model_config:
>         resultset: Directories
>    - type: Submit
>      name: submit
>      value: Submit
>
> Both Selects are pre-populated. The admin user selects a Project to add one
> or more Directories to.
>
> What I can't seem to get my head around is how to tell the config that he
> needs to take one project, take one or more directories, and apply that
> knowledge to the project_directories table. I read about 'type: Repeatable'
> in HTML::FormFu::Model::DBIC, and have played with that, but no soap so far.
>
> /dennis

Okay, I've reread the original question, and understand better what
you're wanting to do.
There's 2 sides to this - filling the form from the database - then
submitting the form and updating the database.

First, when the user selects an option from the first 'projects'
field, are you wanting the 2nd 'directories' field to update itself
with the currently related rows?
If so, you'll need to use JavaScript for that, and there's nothing
really in FormFu that'll help with that part.

Are both Select menus being populated correctly with all the rows from
each table?
If so, then we can move onto handling submitting the form.

Your config looks OK - except the 2nd Select field needs to be named
after the Project's many-to-many relationship - in this case
'directories'.

Then something like this should get the job done:

sub foo : Local : FormConfig {
    my ( $self, $c ) = @_;

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

    if ( $form->submitted_and_valid ) {
        # which project did the user select?
        my $project_id = $form->param_value('project');

        # retrieve the Project row
        my $project = $c->model('Foo')->resultset('Projects')->find(
$project_id );

        # this will remove all related directories from the many-to-many,
        # and then associate all selected directories
        $form->model->update( $project );

        $c->response->redirect('/done');
    }
    elsif ( !$form->submitted ) {
        $form->model->default_values;
    }

    return;
}

Hope this helps!
Carl



More information about the HTML-FormFu mailing list