[html-formfu] How to handle checkboxes

Carl Franks fireartist at gmail.com
Wed Aug 27 14:49:03 BST 2008


2008/8/27 Dermot <paikkos at googlemail.com>:
> Hi,
>
> As a first post I would like to say what a great framework FormFu is.
>
> I have a feeling my first question is not the smartest to ask but here goes.
>
> My form has a series of checkboxs that correspond to true/false fields.
>
> I am doing this to
>
>   if ($c->model->{val} == 1) {
>             $box->attributes->{checked} = 'true';
>   }
>
>
> This gives me a source like this
>
>
> <label>Has type</label>
> <input name="hastype" type="checkbox" checked="true" />
>
>
> My query is whether this is correct HTML (4)?
>
> I had a look at the WC3 site and I think checked is an attribute but I
> thought I should be getting a source like
> <input name="hastype" type="checkbox" checked  />
>
> Am I using the right methods?

Hi Dermot,

Don't worry - this isn't a silly question at all!

As Moritz said, checked="checked" is equivalent to the old `checked`,
and should work just fine in HTML4 - it's just that our output is
geared towards being valid xhtml.

I think there's a more "correct" way of doing this, though.
You should always be setting the Checkbox's value(), so that the
browser knows what to send back when it's checked.
In this case, you could just do this

    $box->default( $c->model->{val} );

And the Checkbox is smart enough to set checked="checked" when
default() is the same as value().
And if they're different, the default() will just be ignored.

Carl



More information about the HTML-FormFu mailing list