Permalink
Browse files

Work around bug with empty row and many_to_many

  • Loading branch information...
1 parent f8c2a68 commit 33de01de6d0c4a670204e466b95f47d279b4a7b6 gerda.shank@gmail.com committed Jan 21, 2009
@@ -9,7 +9,7 @@ use UNIVERSAL::require;
__PACKAGE__->mk_accessors('form_name_space', 'model_name', 'ctx', 'fif');
-our $VERSION = '0.01_01';
+our $VERSION = '0.02';
=head1 NAME
@@ -147,8 +147,9 @@ sub get_form
if ( $package->isa('Form::Processor::Model::DBIC') )
{
# schema only exists for DBIC model
- die "No model to create schema for C::C::F::P" unless $self->model;
- $args{schema} = $self->model->schema;
+ die "Form $package must have an item or a schema (via model_name)"
+ unless ( $self->model || $args{item} );
+ $args{schema} = $self->model->schema if $self->model;
}
return $self->ctx->stash->{form} = $package->new(%args);
@@ -6,7 +6,7 @@ with 'Catalyst::Component::InstancePerContext';
use Carp;
use UNIVERSAL::require;
-our $VERSION = '0.01';
+our $VERSION = '0.02';
=head1 NAME
@@ -155,8 +155,9 @@ sub get_form
if ( $package->isa('HTML::FormHandler::Model::DBIC') )
{
# schema only exists for DBIC model
- die "No model to create schema for C::C::H::F" unless $self->model;
- $args{schema} = $self->model->schema;
+ die "Form $package must have an item or a schema (via model_name)"
+ unless ( $self->model || $args{item} );
+ $args{schema} = $self->model->schema if $self->model;
}
if ( $self->form_posted ) # to allow access to params for form building
{
View
@@ -26,9 +26,6 @@ number of methods and attributes have been renamed for internal
consistency and consistency with Moose usage, and some refactoring has
been done.
-This first version should be considered non-stable, since I am actively
-looking for ways to improve it which may result in some api changes.
-
HTML::FormHandler allows you to define HTML form fields and validators, and will
automatically update or create rows in a database, although it can also be
used for non-database forms.
@@ -767,7 +764,6 @@ attribute.
sub init_from_object
{
my $self = shift;
-
my $item = $self->init_object || $self->item || return;
for my $field ( $self->fields )
@@ -498,6 +498,7 @@ sub init_value
}
elsif ( $field->can('multiple' ) && $field->multiple == 1 )
{
+ return unless $item->id;
my @rows = $item->$name->all;
my @values = map { $_->id } @rows;
return @values;
View
Binary file not shown.
View
@@ -52,18 +52,6 @@ sub chained_uri_for
return $c->uri_for($c->action,$c->req->captures,@_);
}
-=item redirect_to_action
-
- $c->redirect_to_action('User','login');
-
-=cut
-
-sub redirect_to_action {
- my ($c, $controller, $action, @params) =@_;
- $c->response->redirect($c->uri_for($c->controller($controller)->action_for($action), @params));
- $c->detach;
-}
-
=back
@@ -6,7 +6,7 @@ with 'Catalyst::Controller::Role::HTML::FormHandler';
use DateTime;
use BookDB::Form::Book;
-__PACKAGE__->config( model_name => 'DB', form_name_space => 'BookDB::Form' );
+__PACKAGE__->config( form_name_space => 'BookDB::Form' );
=head1 NAME
@@ -64,8 +64,8 @@ sub do_list
sub create : Chained('book_base') PathPart('create') Args(0)
{
my ( $self, $c ) = @_;
- # the $book variable will be undefined, which will cause
- # FormHandler to create the record
+ # Create the empty book row for the form
+ $c->stash( book => $c->model('DB::Book')->new_result({}) );
return $self->form($c);
}
@@ -101,7 +101,6 @@ sub form
my $book = $c->stash->{book};
my $validated = $self->update_from_form( $book, 'Book' );
$c->stash->{form}->action( $c->chained_uri_for->as_string );
-
return if !$validated; # This (re)displays the form, because it's the
# 'end' of the method, and the 'default end' action
# takes over, which is to render the view
@@ -115,10 +114,8 @@ sub form
=item form (without Catalyst plugin)
-Handles displaying and validating the form without Catalyst
-Will save to the database on validation
-You must either put values into your HTML: value="[% form.fif.title %]"
-or set up FillInForm.
+Handles displaying and validating the form without the controller/role
+methods. Will save to the database on validation.
=cut
@@ -154,8 +151,7 @@ sub delete : Chained('item') PathPart('delete') Args(0)
my ( $self, $c ) = @_;
# delete row in database
- my $book = $c->stash->{book};
- $book->delete;
+ $c->stash->{book}->delete;
# redirect to list page
$c->res->redirect( $c->uri_for('list') );
}
@@ -170,9 +166,8 @@ sub view : Chained('item') PathPart('') Args(0)
{
my ( $self, $c, $id ) = @_;
- my $book = $c->stash->{book};
$c->stash->{template} = 'book/view.tt';
- my $validated = $self->update_from_form( $book, 'BookView' );
+ my $validated = $self->update_from_form( $c->stash->{book}, 'BookView' );
return if !$validated;
# form validated
$c->stash->{message} = 'Book checked out';
@@ -184,14 +179,16 @@ sub view : Chained('item') PathPart('') Args(0)
sub do_return : Chained('item') PathPart('return') Args(0)
{
- my ( $self, $c, $id ) = @_;
+ my ( $self, $c ) = @_;
my $book = $c->stash->{book};
$book->borrowed(undef);
$book->borrower(undef);
$book->update;
- $c->res->redirect( $c->uri_for( 'view', $id ) );
+ my $action = $c->controller->action('view');
+ $c->res->redirect($c->uri_for( $action, [$book->id]));
+ $c->detach;
}
=item
@@ -1,6 +1,6 @@
<ul>
<li class="section">Books</li>
- <li><a href="/book/add">Add</a></li>
+ <li><a href="/book/create">Add</a></li>
<li><a href="/book/list">List</a></li>
<li class="section">Borrowers</li>
<li><a href="/borrower/add">Add</a></li>

0 comments on commit 33de01d

Please sign in to comment.