[html-formfu] Modifying values in the form after loaded from DB

Carl Franks fireartist at gmail.com
Sat Apr 4 07:55:41 GMT 2009


2009/4/3 Leanan Sidhe <the.leanan.sidhe at gmail.com>:

Hi,
In response to your previous message - if you're typically needing the
processed / formatted values, you should probably be using DBIx::Class
inflators / deflators.
It's not hard to write custom inflators / deflators - just look at the
code of some on cpan.
The following comments are therefore only if you're determined not to do that ;)

> But now I'm trying to convert from what is in the db, to a more sane value
> for humans to read.  I figured out that I need to use $field->default
> instead of $form->add_valid (I think?).

default() is for use before displaying the form. It's what's used by
model->default_values()

add_valid() is for use after a form has been submitted.

> After that call to $form->model->default_values, what happens?  I see that
> at the end of default values, there is a return $form.  But I don't see
> anywhere that is doing $c->stash->{'form'} = $whatever.  My guess is I'm
> just missing where ever that gets done (or it's some automagical thing).

Any changes you make to the form, work on that same form.
The only reason methods return the form object, is so you can chain
method calls, such as:
    $form->id('foo')->action('/login')->indicator('submit');

Because all perl objects are references, if you do:
    my $form = $c->stash->{form};
then you never need to put the form back on the stash after making
changes to the form, because it's still the same object.

> I ask because in trying to figure out how I should do this, I thought I'd
> look and see how default_values work.  I thought that it'd be as simple as:
>
> $form->model->default_values($c->model('DB::Thing'));
> my $converted_form = do_conversion($form);
> $c->stash->{'form'} = $converted_form;
>
> Or perhaps even:
> $form->model->default_values($c->model('DB::Thing'));
> my $converted_form = do_conversion($c->stash->{'form'});
> $c->stash->{'form'} = $converted_form;

Both of these examples should work, logically, so the problem must be
inside do_conversion().

I'd suggest not returning a new form from do_conversion(), so it
really should just be:
    my $form = $c->c->stash->{form};
    $form->model->default_values( $row );
    do_conversion( $form );

Whatever happens in do_conversion(), you should be getting the value
that model->default_values() set with
    $field->default();
and updating it with
    $field->default( $value );

Carl



More information about the HTML-FormFu mailing list