Catalyst::Manual::Tutorial::AdvancedCRUD 0.02 review

Download
by rbytes.net on

Catalyst::Manual::Tutorial::AdvancedCRUD is a Catalyst Tutorial - Part 8: Advanced CRUD. Add a Form Creation Helper Method Open

License: Perl Artistic License
File size: 126K
Developer: Kennedy Clark
0 stars award from rbytes.net

Catalyst::Manual::Tutorial::AdvancedCRUD is a Catalyst Tutorial - Part 8: Advanced CRUD.

Add a Form Creation Helper Method

Open lib/MyApp/Controller/Books.pm in your editor and add the following method:

=head2 make_book_widget

Build an HTML::Widget form for book creation and updates

=cut

sub make_book_widget {
my ($self, $c) = @_;

# Create an HTML::Widget to build the form
my $w = $c->widget('book_form')->method('post');

# Get authors
my @authorObjs = $c->model("MyAppDB::Author")->all();
my @authors = map {$_->id => $_->last_name }
sort {$a->last_name cmp $b->last_name} @authorObjs;

# Create the form feilds
$w->element('Textfield', 'title' )->label('Title')->size(60);
$w->element('Textfield', 'rating' )->label('Rating')->size(1);
$w->element('Select', 'authors')->label('Authors')
->options(@authors);
$w->element('Submit', 'submit' )->value('submit');

# Return the widget
return $w;
}

This method provides a central location that builds an HTML::Widget-based form with the appropriate fields. The "Get authors" code uses DBIC to retrieve a list of model objects and then uses map to create a hash where the hash keys are the database primary keys from the authors table and the associated values are the last names of the authors.

Requirements:
Perl

Catalyst::Manual::Tutorial::AdvancedCRUD 0.02 keywords