[html-formfu] How to handle checkboxes

Carl Franks fireartist at gmail.com
Thu Aug 28 08:50:06 BST 2008


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?

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.

If you want value() to be set to '1' on all the checkboxes you create,
you can do it the easy way with the following yaml:

default_args:
  elements:
    Checkbox:
      value: 1

> 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

> >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 ;)

Carl



More information about the HTML-FormFu mailing list