[html-formfu] Displaying 2 forms in the same page
Matija Grabnar
matija at serverflow.com
Tue Oct 14 11:24:24 BST 2008
Andreas Marienborg wrote:
> More like this? this is untested, and written by memory :p someone
> might wanna write a test-application based on it?
>
That certainly makes it clearer. (And I'm amazed that I missed the
"indicator" entry while reading the form-fu documentation). Just one
quibble: Shouldn't the comment
# Since we set indicator, we should only end up here if we
# have a username in the form
come AFTER this if?
if ($login_form->submitted_and_valid) {
Or am I completely misunderstanding how and where indicator is used?
> - andreas
>
>
> =head2 Multiple forms using Catalyst::Controller::HTML::FormFu
>
> Sometimes you need to display multiple forms on a single page. If you
> try to use FormConfig on several actions in a chain, or similar, they
> all use $c->stash->{form} to store the form, hence you only get the last
> form.
>
> One way to work around such problems is to do a little of the work
> yourself:
>
> In this example we have a login_form that we want on every page
>
> # root/forms/login.yml:
> ---
> indicator: username
> elements:
> -
> type: Text
> name: username
> constraints:
> - Required
> ...
>
> We also have an edit-form
>
> # root/forms/foo/edit.yml
> ---
> indicator: name
> elements:
> -
> type: Text
> name: name
> constraints:
> - Required
> -
> type: Password
> name: password
> constraints:
> - Required
> ...
>
> We load this in the top-most auto action:
>
> package MyApp::Controller::Root;
>
> use parent 'Catalyst::Controller::HTML::FormFu';
>
> sub auto : Private {
> my ($self, $c) = @_;
>
> # We want to utilize alot of the magic that the controller
> # gives us, so therefore we call $self->form like this
>
> my $login_form = $self->form;
> $login_form->load_config('forms/login.yml');
>
> # Notice how we put it into another stash var, not 'form'
> $c->stash->{login_form} = $login_form;
>
> # Since we set indicator, we should only end up here if we
> # have a username in the form
> $login_form->process();
>
> if ($login_form->submitted_and_valid) {
> $c->authenticate({
> username => $login_form->field_value('username'),
> password => $login_form->field_value('password'),
> });
> }
> }
>
>
> Any other page that wants to load another form, can now do so freely:
>
> package MyApp::Controller::Foo;
>
> sub edit : Local FormConfig {
> my ( $self, $c, $id ) = @_;
>
> my $form = $c->stash->{form};
> my $item = $c->model('DBIC::Foo')->find($id);
> if ($form->submitted_and_valid) {
> # Do whatever you want with it :p
> $form->model->update($item);
> }
> }
>
> In the view-land we now have two stash-variables:
>
> root/foo/edit.tt
> [% login_form %]
> <h2>edit</h2>
> [% form %]
>
>
>
> _______________________________________________
> HTML-FormFu mailing list
> HTML-FormFu at lists.scsys.co.uk
> http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/html-formfu
More information about the HTML-FormFu
mailing list