Cloning a form instance

10 views
Skip to first unread message

Matthias Dietrich

unread,
Feb 4, 2012, 4:47:00 PM2/4/12
to formh...@googlegroups.com
Hi,

I'm using a form to add several items to a list.  Each item then should be displayed as the same form with the values of the item.

Is there a easy way to clone the form and process with each items values, without fetching options for a select from a database again, losing the language handle or creating memory leaks (unweakened references)?  I know modules like Clone::Fast, but don't know if they are appropriate here.

Thanks for response.
  Matthias

-- 
rainboxx Software Engineering
Matthias Dietrich

rainboxx Matthias Dietrich   |  Phone: +49 7141 / 2 39 14 71
Königsallee 43               |  Mobil: +49  151 / 50 60 78 64
71638 Ludwigsburg            |  WWW  :  http://www.rainboxx.de




Gerda Shank

unread,
Feb 4, 2012, 6:51:45 PM2/4/12
to formh...@googlegroups.com
Matthias:

Is there some reason you don't want to create multiple instances of the form?

For example: https://github.com/gshank/formhandler-example/blob/master/lib/MyApp/Controller/Bootstrap.pm
The 'basic' action is creating three instances of MyApp::Form::Basic. Unless there's a lot of overhead in constructing your form, I wouldn't think it would be worth it to clone them.

If you want the options to only load once in that example, you could get them outside FormHandler and pass them into the form.

You don't have to separate out the params in that example, because of the 'html_prefix' setting. That adds a prefix to the field names, so each form will only process its own params.

Gerda
--
You received this message because you are subscribed to the Google Groups "formhandler" group.
To post to this group, send email to formh...@googlegroups.com.
To unsubscribe from this group, send email to formhandler...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/formhandler?hl=en.

Gerda Shank

unread,
Feb 4, 2012, 6:54:46 PM2/4/12
to formh...@googlegroups.com
Also, have you considered using a Repeatable? You can have an array of
compound fields and it would be just one form.

Gerda


On 2/4/12 4:47 PM, Matthias Dietrich wrote:

Matthias Dietrich

unread,
Feb 5, 2012, 5:06:23 AM2/5/12
to formh...@googlegroups.com
Hi,

Am 05.02.2012 um 00:51 schrieb Gerda Shank:

Is there some reason you don't want to create multiple instances of the form?

For example: https://github.com/gshank/formhandler-example/blob/master/lib/MyApp/Controller/Bootstrap.pm
The 'basic' action is creating three instances of MyApp::Form::Basic. Unless there's a lot of overhead in constructing your form, I wouldn't think it would be worth it to clone them.

yes: loading of the options takes quite a bit of time...  I'm doing this currently, but as loading one form takes about 0.05 seconds and there's no limit of the added items, it may take long to generate a bunch of forms (and there are already many other things that needs to be loaded, too).

If you want the options to only load once in that example, you could get them outside FormHandler and pass them into the form.

That could also be an option, but then I need to "copy" the code from my custom select field with optgroups or rebuild it, so I can use it outside of HFH, too.  $hfh->clone would be easier for me ;-).

You don't have to separate out the params in that example, because of the 'html_prefix' setting. That adds a prefix to the field names, so each form will only process its own params.

Luckily I don't need so separate them as each form is submitted on its on via Javascript when a field is changed.  There's no send/save button.


Repeatables: The front-end copies the HTML of the form for each item when it is added.  I could create the repeatables via Javascript, but then the options would be loaded on site reload for each new item, too.

So I see 3 possibilities for me:
 * Rebuild the options-optgroup-loading and load it outside
 * Someone writes a ->clone method or helps me to do so
 * I rebuild my front-end so it copies one form and places the params into the fields

Currently the third variant seems to be the fastest and easiest one.

Thanks,

Gerda Shank

unread,
Feb 6, 2012, 12:23:39 AM2/6/12
to formh...@googlegroups.com
You could take the options that are built on the first copy and pass them into the additional copies:

my $form1 = MyApp::Form->new;
my $options = $form1->field('my_field')->options;
my $form2 = MyApp::Form->new( field_list => [ '+my_field' => { options => $options} ] );
my $form3 = MyApp::Form->new( field-list => [ '+my_field' => { options => $options} ] );

Gerda

Matthias Dietrich

unread,
Feb 6, 2012, 7:50:24 PM2/6/12
to formh...@googlegroups.com
Hi,

Am 06.02.2012 um 06:23 schrieb Gerda Shank:

You could take the options that are built on the first copy and pass them into the additional copies:

my $form1 = MyApp::Form->new;
my $options = $form1->field('my_field')->options;
my $form2 = MyApp::Form->new( field_list => [ '+my_field' => { options => $options} ] );
my $form3 = MyApp::Form->new( field-list => [ '+my_field' => { options => $options} ] );

ah, thanks!  Didn't knew it was possible to merge fields with new.

Best,
  Matthias
Reply all
Reply to author
Forward
0 new messages