Hi,
I'm new to the Catalyst architecture and have been working through some
examples to learn the ropes. However, in the application I am working on, I am
able to Update, but not Create a new record. I am using HTML::FormHandler and I
believe that the inability to Create a new record has something to do with my
setup of the Form Model. Each time I try to create a new record I get the
following error.
Caught exception in Signup::Web::Controller::users->add "Can't call method
"resolve" on an undefined value at
C:/strawberry/perl/site/lib/DBIx/Class/Row.pm line 1289."The error leads me to
believe that the new_result is not working property with the Form, because if I
remove the code and leave the "item" empty the form will generate but of course
not allow the new record to be added. Data::Dumper shows that the Item variable
looks like this:
Item: $VAR1 = bless( {}, 'Signup::Web::Model::signup::User' );
Any idea where I have went wrong so that this error is created when adding new
Records and not updates?
---- HFH Form Object----package Signup::Web::Form::User;
use HTML::FormHandler::Moose;
use namespace::autoclean;
extends 'HTML::FormHandler::Model::DBIC';
has '+item_class' => ( default =>'Signup::User' );
has_field 'alias';
has_field 'name_first' => ( type => 'Text', 'label' => 'First Name' );
has_field 'name_last' => ( type => 'Text', 'label' => 'Last Name' );
has_field 'email' => ( type => 'Text', required => 1 );
has_field 'submit' => ( type => 'Submit', value => 'Submit' );
__PACKAGE__->meta->make_immutable;
1;
---- User Controller Outlining the "Add" actionpackage
Signup::Web::Controller::users;
use Moose;
use namespace::autoclean;
use Signup::Web::Form::User;
BEGIN {extends 'Catalyst::Controller'; }
=head1 NAME
Signup::Web::Controller::users - Catalyst Controller
=head1 DESCRIPTION
Catalyst Controller.
=head1 METHODS
=cut
=head2 index
=cut
sub index :Path('/users') :Args(0) {
my ( $self, $c ) = @_;
$c->load_status_msgs;
my $users = [ $c->model('Signup::User')->all ];
my @columns = ( 'namelast', 'email' );
$c->stash( users => $users, columns => \@columns,
template => 'users/list.tt2' );
}
sub add : Local Args(0) {
my ( $self, $c ) = @_;
## Store the empty record in the "record" variable
## for $self->form
return $self->form( $c );
}
sub edit : Local Args(1) {
my ( $self, $c, $user_id ) = @_;
## Store the user that we are going to edit
$c->stash( record => $c->model('Signup::User')->find($user_id) );
$c->log->debug("RECORD: edit");
return $self->form($c);
}
sub form : Private{
my ( $self, $c ) = @_;
## Check if a record is in the stash, if not create an empty result
my $item = $c->stash->{record} ||
$c->model('Signup::User')->new_result({});
$c->log->debug( "Item: " . Data::Dumper->Dump([ $item ]));
## Create a new form object with the proper Item data
my $form = Signup::Web::Form::User->new( item => $item );
$c->log->debug( "After new form" );
## Set the output template and variables required
$c->stash( template => 'users/form.tt2', form => $form );
$c->log->debug( "Before Process" );
## Validate the form
return unless $form->process( params => $c->req->parameters );
## On Succcessfull Validation redirect to the index page
$c->response->redirect($c->uri_for($self->action_for('index'),
{mid => $c->set_status_msg("User Added")}
));
}
=head1 AUTHOR
A clever guy
=head1 LICENSE
This library is free software. You can redistribute it and/or modify
it under the same terms as Perl itself.
=cut
__PACKAGE__->meta->make_immutable;
1;
_______________________________________________
List: [email protected]
Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/[email protected]/
Dev site: http://dev.catalyst.perl.org/