nybble_1 has asked for the wisdom of the Perl Monks concerning the following question:

Howdy Robed Ones!

I wanted to know if it is possible to pass a DBIx::Class ResultSet into HTML::FormHandler such that all instances are displayed. Presumably these would use the HFH Repeatable functionality.

To put this as a more concrete example, assume a person's phone (work, home ...) with the standard US fields (areacode, number ...). A single phone number row could be coded as such and it works fabulous (I happen to be working with Catalyst, so the code is of that nature):

my $formPhone = MyApp::Form::Phone->new; $formPhone->process(item => $c->model('MyDb')->find(1), params => $c->request->params); $c->stash->{formPhone} = $formPhone;
Corresponding form code:
use HTML::FormHandler::Moose; extends 'HTML::FormHandler'; with 'HTML::FormHandler::Render::Table'; has_field 'area_code' => (type => 'Text', label => 'Area Code', +required => 1, css_class=>'required_field'); has_field 'number' => (type => 'Text', label => 'Home Phone', re +quired => 1, css_class=>'required_field'); __PACKAGE__->meta->make_immutable; 1;
When a ResultSet is passed in:
my $formPhone = MyApp::Form::Phone->new; $formPhone->process(item => $c->model('MyDb')->search(...)->all, params => $c->request->params); $c->stash->{formPhone} = $formPhone;
Corresponding (approximation) repeatable form code :
use HTML::FormHandler::Moose; extends 'HTML::FormHandler'; with 'HTML::FormHandler::Render::Table'; has_field 'phone' => ( type => 'Repeatable' ); has_field 'phone.id' => ( type => 'PrimaryKey' ); has_field 'phone.area_code' => (type => 'Text', label => 'Area Code', +required => 1, css_class=>'required_field'); has_field 'phone.number' => (type => 'Text', label => 'Home Phone', re +quired => 1, css_class=>'required_field'); __PACKAGE__->meta->make_immutable; 1;
the code blows up with:
invalid attribute 'HASH(Ox...)' passed to setup_form at ... FormHandler.pm line 853

What is the right way to do this? (HFH v. 0.30003)

Replies are listed 'Best First'.
Re: HTML::FormHandler, Repeatables and DBIC ResultSet
by actualize (Monk) on Apr 10, 2010 at 20:56 UTC
    You should read the tutorial:

    HTML::FormHandler::Manual::Tutorial

    It will tell you everything you need to know about getting your form to access the database. In order to get access to the database for displaying the form, you have to add the module HTML::FormHandler::Model::DBIC to the form. Just follow the documentation on CPAN and you should be able to do what you want with ease.

    I also see that you are just passing the database hash directly to the process method. I don't think that works. I think you have to assign it to a hash value named schema. Anyway, you should see how to do it in the docs.