Maypole
view release on metacpan or search on metacpan
lib/Maypole/Model/CDBI/FromCGI.pm view on Meta::CPAN
sub cgi_update_errors { %{ shift->{_cgi_update_error} || {} } }
=head2 create_from_cgi
Based on the same method in Class::DBI::FromCGI.
Creates multiple objects from a cgi form.
Errors are returned in cgi_update_errors
It can be called Maypole style passing the Maypole request object as the
first arg, or Class::DBI::FromCGI style passing the Untaint Handler ($h)
as the first arg.
A hashref of options can be passed as the second argument. Unlike
in the CDBI equivalent, you can *not* pass a list as the second argument.
Options can be :
params -- hashref of cgi data to use instead of $r->params,
required -- list of fields that are required
ignore -- list of fields to ignore
all -- list of all fields (defaults to $class->columns)
=cut
sub create_from_cgi {
my ($self, $r, $opts) = @_;
$self->_croak( "create_from_cgi can only be called as a class method")
if ref $self;
my ($errors, $validated);
if ($r->isa('CGI::Untaint')) { # FromCGI interface compatibility
($validated, $errors) = $self->validate_inputs($r,$opts);
} else {
my $params = $opts->{params} || $r->params;
$opts->{params} = $self->classify_form_inputs($params);
($validated, $errors) = $self->validate_all($r, $opts);
}
if (keys %$errors) {
return bless { _cgi_update_error => $errors }, $self;
}
# Insert all the data
my ($obj, $err ) = $self->_do_create_all($validated);
if ($err) {
return bless { _cgi_update_error => $err }, $self;
}
return $obj;
}
=head2 update_from_cgi
Replicates the Class::DBI::FromCGI method of same name. It updates an object and
returns 1 upon success. It can take the same arguments as create_form_cgi.
If errors, it sets the cgi_update_errors.
=cut
sub update_from_cgi {
my ($self, $r, $opts) = @_;
$self->_croak( "update_from_cgi can only be called as an object method") unless ref $self;
my ($errors, $validated);
$self->{_cgi_update_error} = {};
$opts->{updating} = 1;
# FromCGI interface compatibility
if ($r->isa('CGI::Untaint')) {
# REHASH the $opts for updating:
# 1: we ignore any fields we dont have parmeter for. (safe ?)
# 2: we dont want to update fields unless they change
my @ignore = @{$opts->{ignore} || []};
push @ignore, $self->primary_column->name;
my $raw = $r->raw_data;
#print "*** raw data ****" . Dumper($raw);
foreach my $field ($self->columns) {
#print "*** field is $field ***\n";
if (not defined $raw->{$field}) {
push @ignore, $field->name;
#print "*** ignoring $field because it is not present ***\n";
next;
}
# stupid inflation , cant get at raw db value easy, must call
# deflate ***FIXME****
my $cur_val = ref $self->$field ? $self->$field->id : $self->$field;
if ($raw->{$field} eq $cur_val) {
#print "*** ignoring $field because unchanged ***\n";
push @ignore, "$field";
}
}
$opts->{ignore} = \@ignore;
($validated, $errors) = $self->validate_inputs($r,$opts);
} else {
my $params = $opts->{params} || $r->params;
$opts->{params} = $self->classify_form_inputs($params);
($validated, $errors) = $self->validate_all($r, $opts);
#print "*** errors for validate all ****" . Dumper($errors);
}
if (keys %$errors) {
#print "*** we have errors ****" . Dumper($errors);
$self->{_cgi_update_error} = $errors;
return;
}
# Update all the data
my ($obj, $err ) = $self->_do_update_all($validated);
if ($err) {
$self->{_cgi_update_error} = $err;
return;
}
return 1;
}
=head2 add_to_from_cgi
$obj->add_to_from_cgi($r[, $opts]);
Like add_to_* for has_many relationships but will add nay objects it can
( run in 1.096 second using v1.01-cache-2.11-cpan-e1769b4cff6 )