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

This is a second attempt to succinctly state this issue. For those who feel the need to see further code, my complete (non-working) subroutine is posted at:
http://perlmonks.com/?node_id=763253

I am working to generate a form dynamically from database calls.

I need to generate a form checkbox for each phone record pulled from my query. I would have thought that setting my %row_data{'CHECKBOX'} to a $form->field() object would have done the trick, but apparently not. Data::Dumper says it is: "'CHECKBOX' => 'perform_group_action'", instead of an entire form element. Commenting out the 'value' key leaves CHECKBOX undefined, commenting out the 'options' key throws errors reading: "No options specified for 'checkbox' field".

The name of the checkbox field changes on each iteration through the list. How do I invoke a "<tmpl-var field-checkbox>" with a changing fieldname and embedded in rows of my template?

I'm looking for a clue here. Anybody have one to spare?

-- Hugh

My template reads:

<DIV align="right" id="GroupAction" class="GroupAction" title="Group_A +ction"> <h3>Group Actions:</h3> <table> <tmpl_var form-start> <tr><td colspan="2"></td><td colspan="2">Call Group: <tmpl_var field-g +roup> Group Action: <tmpl_var field-group_action></td><td colspan="2" +><tmpl_var form -submit></td></tr> <tr><th>Tag</th><th>First Name</th><th>Last Name</th><th>Email</th><th + +>Phone</th><th>Actions</th></tr> <tmpl_loop name="PHONES"> <tr><td> <tmpl-var field-checkbox> </td><td> <tmpl-var name=FNAME> </t +d><td> <tmpl-var name=LNAME> </td><td> <tmpl-var name=EMAIL> </td><td +> <tmpl-var name=PHONE> </td><td> <tmpl-var name=LINKS> </td></tr> </tmpl_loop> <tmpl_var form-end> INSERT_NEW_RECORD_HERE </table> </DIV>
------------------------------------

And the problematic portion of my code reads:

my @loop_data; while (my $phone = $sth->fetchrow_hashref()){ my %row_data; my $name = 'group_action_' . $phone->{'phone_number_id'}; $row_data{'CHECKBOX'} = $form->field( name => $name, label => '', type => 'checkbox', options => [ 'perform_group_action' ], sticky => 0, value => 'perform_group_action', ); $row_data{'FNAME'} = $phone->{'fname'}; $row_data{'LNAME'} = $phone->{'lname'}; $row_data{'EMAIL'} = $phone->{'email'}; $row_data{'PHONE'} = $phone->{'phone'}; $row_data{'LINKS'} = $self->_get_links_for_phone_list( $phone->{'p +hone_number_id'} ); push (@loop_data, \%row_data); } $form->tmpl_param(PHONES => \@loop_data); print STDERR "The loop_data incldes: \n" . Dumper(\@loop_data);
if( $lal && $lol ) { $life++; }

Replies are listed 'Best First'.
Re: CGI::FormBuilder, HTML::Template and \@loop_data question; trying again
by Anonymous Monk on May 14, 2009 at 04:24 UTC
    Here is a tip. Present your problem like this
    #!/usr/bin/perl -- use strict; use warnings; use CGI::FormBuilder; my $form = CGI::FormBuilder->new(template => 'test.tmpl'); my @loop_data = DATA::DUMPER OUTPUT HERE!!!!!; print 'wrong output ', $template->tmpl_param(PHONES => \@loop_data); __END__ wrong output here
    and explain how the wrong output you receive differs from what you want.
Re: CGI::FormBuilder, HTML::Template and \@loop_data question; trying again
by hesco (Deacon) on May 14, 2009 at 14:30 UTC
    My issue here does not seem to be with CGI::FormBuilder, which is appropriately creating those checkboxes as requested. See Data::Dumper output snippet here:
    'fieldrefs' => { 'group_action_94' => bless( { 'cleanopts' => 1, 'fieldrefs' => {}, 'options' => [ 'perform_group_action' ], 'value' => 'perform_group_action', 'name' => 'group_action_94', 'selectname' => 1, 'sticky' => 0, 'required' => 0, '_form' => ${$VAR1}, 'type' => 'checkbox', 'label' => '', 'selectnum' => 5, 'validate' => undef }, 'CGI::FormBuilder::Field' ),
    My issue is this: How do I invoke in my template a "<tmpl-var field-checkbox>" with a variable fieldname, which changes on each iteration through the loop and have my dynamically generated checkbox embedded in rows of my output?

    Do I need to extend HTML::Template to add a new tag to accommodate a variable field name? And if so, can anyone with knowledge of its innards offer guidance about how best to accomplish that? And if not, how would this be done?

    FYI: Save the fact that perlmonks does not want to seem to want me to embed a form into this node, this is the result I'm hoping to see:

    <form action="dashboard.cgi" id="Group_Actions" method="post" name="Group_Actions">
    Call Group: <select id="group" name="group">
    <option value="">-select-</option>
    <option value="7">test_group_001</option>
    <option value="14">test_group_007</option>
    <option value="15">test_group_008</option>
    </select>
    Group Action: <select id="group_action" name="group_action">
    <option value="">-select-</option>
    <option value="edit_group">Edit Group</option>
    <option value="add_to_group">Add to Group</option>
    <option value="remove_from_group">Remove from Group</option>
    <option value="delete_from_list">Delete from List</option>
    </select>
    <input id="Group_Actions_submit" name="_submit" type="submit" value="Process Group Action" />
    TagFirst NameLast NameEmailPhoneActions
    <input type="checkbox" name="group_action_94" value="perform_group_action"> Testy Tester tt@example.com 123-555-1212 some links
    <input type="checkbox" name="group_action_95" value="perform_group_action"> Testoria Tester tt2@example.com 321-555-1212 some links
    </form>

    Please note: that the field name changes for each checkbox in the tag column; and that the number of rows in this table can not be predicted and will change with user input.

    -- Hugh Esco

    if( $lal && $lol ) { $life++; }
Re: CGI::FormBuilder, HTML::Template and \@loop_data question; trying again
by hesco (Deacon) on May 29, 2009 at 22:08 UTC