[html-formfu] How to handle checkboxes

Dermot paikkos at googlemail.com
Wed Aug 27 16:10:35 BST 2008


2008/8/27 Carl Franks <fireartist at gmail.com>:
> 2008/8/27 Dermot <paikkos at googlemail.com>:
>> 2008/8/27 Carl Franks <fireartist at gmail.com>:
>>> 2008/8/27 Dermot <paikkos at googlemail.com>:
>>>> Hi,
>> use base qw(Catalyst::Controller::HTML::FormFu);
>> ...
>>
>> sub updateCheckboxes : Private {
>>  my ($c,$data) = @_;             # Data contains the model.
>>  my $form = $c->stash->{form};
>>  my $checkboxes = $form->get_all_elements({type => 'Checkbox'});
>>  foreach my $box (@{$checkboxes}) {
>>          my $name = $box->name;
>>          my $val = $data->sub_id->$name;
>>          $c->log->debug("Box=$box->{name} $data->sub_id->$box->name
>> VAL=$val");
>> #         if ($val && $val == 1) {
>>          $box->default($val);
>> #         }
>>  }
>> }
>
> No, if value() eq default(), you'll get:
> <input type="checkbox" name="hastype" checked="checked"/>
> otherwise, you'll get:
> <input type="checkbox" name="hastype"/>
>
> Is that what was happening?
>
> Carl


Yes that's what's happening but it indicates an error somewhere.

Some of the data ($data>sub_id->$name) will be either 1 or undef. In
it's original form my subroutine would display checked="true" when the
value ==1. Now none of the checkboxs are displayed as checked.

I can't find the perldoc for this default method you've mentioned.
sub_id is a foriegn key, and $name contains the checkbox name
('hastype'). I didn't think I could use $form->model->default_values()
in this context which is why I used the attributes method.

Sorry, am I confusing matters? What I am trying to do is, loop through
all the checkbox elements, take the name, cross-reference that name
(hastype1, hastype2..etc) in a different table by using the foriegn
key, sub_id. If sub_id->hastype1 == 1, the checkbox should be checked,
otherwise not.

>From a FormFu point of view the HTML is rendered correctly when I used
attributes.

Am I really off-track by using attributes rather than default?
Thanx,
Dp.



More information about the HTML-FormFu mailing list