Class::DBI::FromForm - Update Class::DBI data using Data::FormValidator or HTML Widget


Class-DBI-FromForm documentation Contained in the Class-DBI-FromForm distribution.

Index


Code Index:

NAME

Top

Class::DBI::FromForm - Update Class::DBI data using Data::FormValidator or HTML Widget

SYNOPSIS

Top

  package Film;
  use Class::DBI::FromForm;
  use base 'Class::DBI';

  my $results = Data::FormValidator->check( ... );
  my $film = Film->retrieve('Fahrenheit 911');
  $film->update_from_form($results);

  my $new_film = Film->create_from_form($results);

DESCRIPTION

Top

Create and update Class::DBI objects from Data::FormValidator or HTML::Widget.

METHODS

create_from_form

Create a new object.

update_from_form

Update object.

fill_widget <widget>

Top

This only applies to HTML::Widget>. Fills the form from a CDBI object.

SEE ALSO

Top

Class::DBI, Class::DBI::FromCGI, Data::FormValidator

AUTHOR

Top

Sebastian Riedel, sri@oook.de

LICENSE

Top

This library is free software . You can redistribute it and/or modify it under the same terms as perl itself.


Class-DBI-FromForm documentation Contained in the Class-DBI-FromForm distribution.
package Class::DBI::FromForm;

use strict;
use vars qw/$VERSION @EXPORT/;
use base 'Exporter';

$VERSION = 0.04;

@EXPORT = qw/update_from_form create_from_form/;

sub create_from_form {
    my $class = shift;
    die "create_from_form can only be called as a class method" if ref $class;
    __PACKAGE__->_run_create( $class, @_ );
}

sub update_from_form {
    my $self = shift;
    die "update_from_form cannot be called as a class method" unless ref $self;
    __PACKAGE__->_run_update( $self, @_ );
}

sub _run_create {
    my ( $me, $class, $results ) = @_;
    
    my $them = bless {}, $class;
    my $cols = {};
    foreach my $col ( $them->columns('All') ) {
        if($results->isa('HTML::Widget::Result')) {
            $cols->{$col} = $results->param($col);
        } else {
            $cols->{$col} = $results->valid($col);
        }
    }
    return $class->create($cols);
}

sub _run_update {
    my ( $me, $them, $results ) = @_;
    my @cols = ( $results->isa('HTML::Widget::Result') ?
        $results->valid :
        keys %{ $results->valid } );
        
    foreach my $col ( @cols ) {
        if ( $them->can($col) ) {
            next if $col eq $them->primary_column;
            if($results->isa('HTML::Widget::Result')) {
                $them->$col( $results->param($col));
            } else {
                $them->$col( $results->valid($col));
            }
        }
    }
    $them->update;
    return 1;
}


sub fill_widget {
    my ($me ,$widget)=@_;

    foreach my $element ( @{ $widget->{_elements} } ) {
        my $name=$element->name;
        next unless $name && $me->can($name);
        $element->value($me->$name);
    }
}                                                                                                                                                                          

1;