[html-formfu] checkbox strangeness
Carl Franks
fireartist at gmail.com
Mon Aug 25 09:35:18 BST 2008
2008/8/24 Geoffrey D. Bennett <g at netcraft.com.au>:
> Hi there,
>
> I have a form like this:
>
> indicator: submit
> elements:
> - type: Text
> name: name
> label: Name
> - type: Checkbox
> name: active
> label: Active
> - type: Submit
> name: submit
> value: Submit
>
> which is intended for editing a row from a table like this:
>
> CREATE TABLE org_type (
> id INT PRIMARY KEY DEFAULT nextval('org_type_id_seq'),
> name VARCHAR(80) NOT NULL UNIQUE
> active BOOLEAN NOT NULL DEFAULT TRUE,
> );
>
> The problem is that the checkbox state wasn't being loaded correctly
> from the database or being saved correctly back to it, although the
> text field was fine.
>
> This is how I solved it:
>
> - type: Checkbox
> name: active
> label: Active
> value: 1
> default_empty_value: 1
> inflator: Checkbox
>
> -----8<-----8<-----8<-----8<-----8<-----8<-----
> package HTML::FormFu::Inflator::Checkbox;
>
> use strict;
> use warnings;
> use base 'HTML::FormFu::Inflator';
> use Class::C3;
>
> sub inflator {
> my($self, $value) = @_;
>
> $value ? 1 : 0;
> }
>
> 1;
> -----8<-----8<-----8<-----8<-----8<-----8<-----
>
> All this seems a bit excessive for what I would have thought would be
> a reasonably common case?
>
> I've now got:
>
> default_args:
> elements:
> Checkbox:
> value: 1
> default_empty_value: 1
> inflator: Checkbox
>
> But don't those seem like reasonable defaults anyway?
You should always be setting 'value' for checkboxes - so that the
browser knows what to send if the checkbox is checked.
How are you updating the database?
HTML-FormFu-Model-DBIC handles checkboxes correctly, without needing
'default_empty_value'.
If you're using hand-rolled code to update the database - be aware
that the browser doesn't send anything back for unchecked checkboxes -
not even an empty value - so either your db-update code will need to
check the FormFu form object for checkboxes that haven't been
submitted, or you'll need to use 'default_empty_value'.
Carl
More information about the HTML-FormFu
mailing list