[html-formfu] How to handle checkboxes

Carl Franks fireartist at gmail.com
Thu Aug 28 10:44:54 BST 2008


2008/8/28 Dermot <paikkos at googlemail.com>:
> 2008/8/28 Carl Franks <fireartist at gmail.com>:
>>
>> Are you setting value(1) on all the checkboxes?
>
> No I am not. Here is the debug output I get from this code snippet:

> 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?

Yes - you're mistaken ;)

It'll only give you checked="checked" if value() equals default()

That's the important part ^^^

If you see checkboxes as simply true/false boolean - and you're using
Catalyst-Controller-HTML-FormFu, then you can simply make all
checkboxes have a value="1" by setting this in your myapp.yml config
file:

Controller::HTML::FormFu:
  constructor:
    default_args:
      elements:
       Checkbox:
         value: 1

If you set that, the problem will be solved!

The reason that's not a default, is that checkboxes aren't always a
simple boolean - and it's so simple to set your own application-wide
defaults using the above construct.

<sidetrack>
Ok, I had been mistaken that the browser wouldn't send anything if a
checkbox doesn't have a value.
I've just checked all the browsers at my disposal, and they all send
the value "on" if there's no explicit value - so at least they're
consistent.
However - I still don't think it's good to rely on that behaviour -
particularly if you intend on using HTML-FormFu-Model-DBIC.

If the checkbox relates to a BOOL column, then the checkbox should
have value="1", so that Model-DBIC can update the database correctly.
</sidetrack>

btw - If you do want to try using Model-DBIC for this - can you start
a new thread, and post the relevant DB schema and form config - I
couldn't quite follow your earlier description, so I'm not sure
whether Model-DBIC can do what you want (yet) ;)

Carl



More information about the HTML-FormFu mailing list