FAQ
This ties in to my earlier question regarding converting values that are
submitted before placing them in the db. I figured out how to go about
doing this just fine, thanks to the Clarification of add_valid and me
realizing my controller wasn't calling things in the proper order.

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?).

Here's what I'm doing:

if ($form->submitted_and_valid) {
#process it, stuff it in the db
} else {
$form->model->default_values($c->model('DB::Thing'));
}

I've been looking through the code for HTML::FormFu, HTML::FormFu::Model,
HTML::FormFu::Model::DBIC, Catalyst::Controller::HTML::FormFu, etc.

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).

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;

But neither of those seems to work. The best I've been able to get, no
matter what I try, is that the form contains what is in the db
pre-conversion, or the form contains a bunch of empty data. What am I
missing?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.scsys.co.uk/pipermail/html-formfu/attachments/20090403/825dcb5c/attachment.htm

Search Discussions

  • Carl Franks at Apr 4, 2009 at 8:55 am
    2009/4/3 Leanan Sidhe <the.leanan.sidhe@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

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouphtml-formfu @
categoriesperl, catalyst
postedApr 3, '09 at 6:19p
activeApr 4, '09 at 8:55a
posts2
users2
websitemetacpan.org...

2 users in discussion

Leanan Sidhe: 1 post Carl Franks: 1 post

People

Translate

site design / logo © 2022 Grokbase