[Catalyst] Problem with checkboxes and fillinform plugin

Ian Docherty icd at ecisecurity.com
Fri Feb 17 21:15:51 CET 2006


Hi.
On advice from this list (thank you Bill Moseley) I have been looking at 
the Plugin combo FormValidator and FillInForm

I have a problem with checkboxes that the code below demonstrates.

The issue is that when I am editing an existing user record and use a 
checkbox to display and modify the state of a boolean, if the form is 
re-submitted then FillInForm does not correctly handle checkboxes that 
are set initially to 'checked'.

The reason for this is that FillInForm looks through the request param 
array to decide what input fields it should populate. However in the 
case of checkboxes, an unchecked checkbox returns nothing in the request 
params so FillInForm does not know anything about it. If the original 
state of the checkbox is unchecked this does not matter, if the original 
state is checked then it does matter.

To demonstrate the problem make one of the fields (e.g. the surname) 
blank and submit the form to get an error response, try different 
combinations of state for the checkboxes. The state of the second 
checkbox is remembered correctly, the state of the first is not.

I suppose I can do a quick and dirty work-around by passing the state of 
the 'action' to the template which, if it is a re-submission, does not 
populate the checkbox state and so allows FillInForm to do it's magic. 
But it feels like a cludge (the opposite of a hack).

######

### Admin::C::User::Test ###############################
package Admin::C::User::Test;

use strict;
use warnings;

use base 'Catalyst::Controller';

sub test : Path {
    my ($self, $c) = @_;

    # Get user from database (for test purposes create it here)
    my $user = {
        surname     => 'Docherty',
        firstname   => 'Ian',
        akas        => [
            {name       => 'icydee',    active  => 1},
            {name       => 'icd',       active  => 0},
            ],
        };

    # Check if the file has been submitted
    if ($c->request->param('action')) {
        # Do FormValidator check
        $c->form(
            required => [qw(firstname surname)],
            optional => [qw(akaName0 akaName1 akaCheckbox0 akaCheckbox1)],
            missing_optional_valid => 1,
            );

        $c->stash->{message} = $c->form->success ? 'Success' : 'Failure';
    }
    
    $c->stash->{user} = $user;
    
    $c->stash->{template} = "user/test.tt";
}
1;


### user/test.tt ###############################
<html>
<head><title>Test</title>
</head>
<body>
  <h1>Test script</h1>
  Message [% message %]<p/>
  <form action="/admin/user/test" method="get">
    <input type="hidden" name="action" value="1">

    firstname: <input type="text" name="firstname" value="[% user.firstname
%]"><br/>
    surname: <input type="text" name="surname" value="[% user.surname %]"><br/>
[% FOR aka IN user.akas %]
    aka [% loop.index %]: <input type="text" name="akaName[% loop.index %]"
value="[% aka.name %]">
    <input type="checkbox" name="akaCheckbox[% loop.index %]" value="1" [% IF
aka.active %]checked="checked"[% END %]><br/>
[% END %]
    <input type="submit">
  </form>
</body>
</html>









More information about the Catalyst mailing list