[html-formfu] checkbox strangeness

Geoffrey D. Bennett g at netcraft.com.au
Sun Aug 24 09:13:07 BST 2008


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?

Regards,
-- 
Geoffrey D. Bennett, RHCE, RHCX               mailto:g at netcraft.com.au
Senior Systems Engineer                          sip:g at netcraft.com.au
NetCraft Australia Pty Ltd        http://www.netcraft.com.au/geoffrey/



More information about the HTML-FormFu mailing list