[html-formfu] How to handle checkboxes
Dermot
paikkos at googlemail.com
Thu Aug 28 09:30:32 BST 2008
2008/8/28 Carl Franks <fireartist at gmail.com>:
> 2008/8/27 Dermot <paikkos at googlemail.com>:
>> 2008/8/27 Carl Franks <fireartist at gmail.com>:
>>>
>>> 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.
>
> Are you setting value(1) on all the checkboxes?
No I am not. Here is the debug output I get from this code snippet:
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("$name VAL=$val");
$box->default($val);
}
[debug] Catalyst::Controller::HTML::FormFu::Action::FormConfig
searching for file ....
[debug] hastype1 VAL=
[debug] hastype2 VAL=
[debug] hastype3 VAL=
[debug] hastype4 VAL=1
[debug] hastype5 VAL=1
[debug] hashtype6 VAL=1
[debug] hastype7 VAL=1
[debug] hastype8 VAL=1
[debug] hastype9 VAL=1
My form is rendered with all checkbox's unchecked.
Am I mistaken in thinking a true value for a checkbox element should
render 'checked="checked"' and an undef (or 0) will render the
checkbox unchecked?
> It'll only give you checked="checked" if value() eq default()
> You should be setting a value() on all checkboxes anyway, so that the
> browser does something sane.
Fair enough.
>> 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.
>
> Both value() and default() are documented in:
> lib/HTML/FormFu/Element/_Field.pm
Thanks that's useful.
>> >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?
>
> Setting the attributes will only work so far - if you look at
> prepare_attrs() in Checkbox.pm, there's a lot of logic to ensure it
> works properly.
> And several fields override either process_value() or prepare_attrs()
> for specialized behaviour.
> So - if you can, I'd encourage you to try and get it working without
> setting attributes() yourself - but I know that at the end of the day,
> you just need to get it working ;)
I think I may have to go this way for the moment as I can't seem to
get the desired result from $checkbox->value();
Thanx,
Dp.
More information about the HTML-FormFu
mailing list