[html-formfu] Best practice for multiple same forms

Carl Franks fireartist at gmail.com
Fri Nov 23 15:01:22 GMT 2007


On 23/11/2007, Moritz Onken <onken at houseofdesign.de> wrote:
> Hi,
>
> I need a form which is made up of many forms of same type (same yml
> file). Like phpmyadmin, where you can submit more than one entry on a
> page.
>
> Is there a nice way to create and validate these forms easily? Maybe the
> nested_name stuff could help, but I have no idea yet how to implement
> this.

Do you need a single form, containing lots for repeated parts.
Or multiple similar forms on a single page?

If it's a single form with repeated similar parts, you could use the
new Repeatable block element.
It's use might still change, while I'm working on the new Model/DBIC
feature - so you probably want to hold off until it's merged back into
trunk - it'll hopefully be within the next week or two.

If you want to look at it just now, check out the version at
branches/HTML-FormFu-Model-DBIC/lib/HTML/FormFu/Model/DBIC.pm
as the one in trunk is out of date.

The way you use it (at the moment) is:

    elements:
      - type: Repeatable
        increment_field_names: 1
        query_param: count
        elements:
          - name: foo
          - name: bar
      - type: Hidden
        name: count
      - type: Submit

Then, if you call repeat() on the block, with the number of
repetitions you want, it'll render that number of times.

    $form->get_all_element({ type => 'Repeatable' })->count( 2 );

and the form will render something like this:

<form>
<div>
  <input name="foo_1" type="text" />
  <input name="bar_1" type="text" />
</div>
<div>
  <input name="foo_2" type="text" />
  <input name="bar_2" type="text" />
</div>
<input name="count" type="hidden" />
<input type="submit" />
</form>

Notice that setting increment_field_names(1) causes a counter to be
appended to the field names.

I also set query_param() to the name of a Hidden field outside of the
repeatable block.
After calling
    $element->count( 2 );
you should also then set the Hidden field's default value to the same number:
    $form->default_values({ count => 2 });
This counter will then be sent back by the browser, and then during
$form->process(), this counter is automatically used to call
$element->count() for you, setting up the correct number of fields, so
that constraints, etc will be correctly called.

(I don't think, though, that constraints are working quite right yet,
which is why it's probably best to hold off for now).

Then, when you want to the the submitted values, you could iterate
over the fields, and use $field->original_name() to get hold of the
field name without the "_1" counter on the end.

    my $repeatable = $form->get_all_element({ type => 'Repeatable' });

    for my $field ( @{ $repeatable->get_fields } ) {
        my $name = $field->original_name;
        my $value = $form->param( $name );
    }

Is this the kind of thing you're looking for?

Carl



More information about the HTML-FormFu mailing list