[html-formfu] Nested forms using has_many relationships from DBIC

Henning Kulander hennikul at linpro.no
Thu Dec 13 15:22:48 GMT 2007


Hi!

I'm trying to get the new nested functionality for has_many
relationships to work in HTML::FormFu 0.2001.
I have created a yaml-file which configures the relationship like this:
       - type: Repeatable
         nested_name: accessipranges
         name: ipranges
         increment_field_names: 1
         db:
           new_empty_row: ['fromip', 'toip']
         elements:
          - type: Hidden
            name: id
            
          - type: Text
            name: fromip

          - type: Text
            name: toip

The accessipranges is a has_many relationship to a table called
Accessipranges, and calling that as a method in Perl, DBIC starts the
correct SELECT and retrieves the values. But in HTML::FormFu it does
not. I traced this problem and made this patch to DBIC.pm:
--- DBIC.pm     2007-12-13 13:52:19.000000000 +0100
+++ /home/hk/Desktop/DBIC.pm    2007-12-13 15:58:24.000000000 +0100
@@ -61,14 +63,11 @@
            # check there's a field name matching the PK
 
            my ($pk) = $rs->related_source($rel)->primary_columns;
+
            next
-                unless grep {
-                $pk eq
-                    ( defined $_->original_name ? $_->original_name :
$_->name )
-                } @{ $block->get_fields( { type => 'Hidden' } ) };
-#            next
-#                unless grep { $_->name eq $pk }
-#                @{ $block->get_fields( { type => 'Hidden' } ) };
+                unless grep { $_->name eq $pk }
+                @{ $block->get_fields( { type => 'Hidden' } ) };
+
            my @rows = $dbic->related_resultset($rel)->all;
            my $count
                = $block->db->{new_empty_row}

The patch replaces the old nest unless id is available as a Hidden field
with the one used further down in the module when filling forms with
many_to_many relationships. Now it seems to work.

Now I get a form with all related Accesipranges objects. But when I save
the form, only the first one is processed. That is, if I update the
second object, validators and condition checks are not run on that
object, it is not saved either. If I update the first object, it is
validated and saved to the database using an UPDATE. Creating new
objects works aswell, but only if the empty object is first.. :)

The nested forms are a really usefull functionallity!

Some other caveats I found:
1. The requirement for a hidden id for related objects does not seem to
be documented.
2. Related objects with combined primary key (key using more that one
column for uniqueness) does not seem to work. Should it? It would be
usefull when working with legacy database schemas.


Regards,
Henning Kulander




More information about the HTML-FormFu mailing list