[CDBI] CDBI::FromForm and non-column methods

Bill Moseley moseley at hank.org
Thu Oct 6 22:19:48 BST 2005


CDBI::FromForm does this:

    sub _run_update {
        my ( $me, $them, $results ) = @_;
        foreach my $col ( keys %{ $results->valid } ) {
            if ( $them->can($col) ) {
                next if $col eq $them->primary_column;
                my $val = $results->valid($col);
                $them->$col($val);
            }
        }
        $them->update;
        return 1;
    }

Unfortunately, that "can" check finds methods that are not columns in
that table.  I'm wondering if that shouldn't be a check against
$them->columns instead of calling "can".

An example might be a User form that has checkboxes for their "Roles"
where the role table is related to the user table via a mapping table.

CDBI is nice and sets up the mapping so I can do:

    @roles = $user->roles;

But then if I have "roles" as a field in my form I get an error:

    my $results = Data::FormValidator->check( 
        { roles => [1,4] },
        { required => ['roles'] }
    );

    User->retrieve( 1 )->update_from_form( $results );

Can't coerce array into hash at /usr/local/share/perl/5.8.7/Class/DBI/Sweet.pm line 198.

See any reason not to use $them->columns instead of $them->can?



I can hear someone suggesting to use another name for my "roles"
field, but I need (want) to use the actual name because my template
widget generates the list of choices automatically by calling
"available_items" on the class.

    [% MACRO radio_select(name) WRAPPER div_required %]
        [% FOR i = item.available_items( name ) %]
        <label for="[% name _ i.id %]" class="labelRadio">
            <input type="radio" name="[% name %]" id="[% name _ i.id %]" class="inputRadio" value="[% i.id %]">[% i.name | html %]
        </label>
        [% END %]
    [% END %]


I suppose the other option is to just trim the DFV hash returned by
$results->valid.



Anyone else hate forms?



-- 
Bill Moseley
moseley at hank.org





More information about the ClassDBI mailing list