[html-formfu] update Checkbox field problem

Carl Franks fireartist at gmail.com
Fri Mar 12 14:07:39 GMT 2010


On 12 March 2010 12:55, Stephen Shorrock
<stephen.shorrock at googlemail.com> wrote:
> Dear Carl,
>
> I was wondering whether you would be able to offer some insight into what
> the issue with my code could be:
>
> I'm attempting to update the value of a check box following a form submit:
>
> Along the lines of
>
> #values hardcoded for example
>
> if(defined($c->stash->{form}->get_field("keep_row"))){
>               print STDERR $form->get_field("keep_row)."\n";
>           $form->get_field("keep_row")->value("118");
>               print STDERR $form->get_field("keep_row")."\n";
>           $form->get_field("keep_row")->add_attrs({checked=>'checked'});
>               print STDERR $form->get_field("keep_row")."\n";
> }
>
> When the checkbox has been checked the checked attribute is removed:
>
> <div class="checkbox"><input name="keep_row" type="checkbox" value="1"
>  checked="checked" /></div>
> <div class="checkbox"><input name="keep_row" type="checkbox" value="118"
> /></div>
> <div class="checkbox"><input name="keep_row" type="checkbox" value="118"
> /></div>
>
> However when the checkbox has not been selected and I want to update then I
> am able to add the checked attribute and modify the value (I'm essentially
> populating the form with database defaults).
>
> <div class="checkbox"><input name="keep_row" type="checkbox" value="1"
>  /></div>
> <div class="checkbox"><input name="keep_row" type="checkbox" value="118"
> /></div>
> <div class="checkbox"><input name="keep_row" type="checkbox" value="118"
>  checked="checked" /></div>
>
> I've tried to debug by writing a test (reading from a yml) with checkbox
> attribute set or unset in the yml file, but this seems to work fine.
>
> Is modifying the field and its attributes the correct way for me to build
> the form, or are there some quirks because I'm trying to modify already
> processed fields? No Filters/Constraints/Plugins exist on the Checkbox field

Hi,

You shouldn't try to set the 'checked' attribute yourself.
Generally, use $field->default()
If default() == value(), then render will set 'checked' for you.

In this case, because the form has been submitted, it'll compare the
submitted value against the new $field->value(), and because they're
not the same, it'll unset the 'checked' attribute.

If this is purely for re-rendering,
I'd suggest making the form think it hasn't been submitted, and then
setting the default values.
Either create a new form object from the original config file, or
    $form->query(undef);
    $form->process();
should do it.

If that's not acceptable, then I think you'll need to do this:

    my $value = 118;

    $field->default( $value );

    $form->set_nested_hash_value(
        $form->input,
        $field->nested_name,
    );

Even if it is a bit of a hack ;)

The behaviour you're seeing is due to prepare_attrs() in Checkbox.pm

Cheers,
Carl



More information about the HTML-FormFu mailing list