Often you want the user to have a form not just for one entity but for all of them on the same page. A form for each product in the same category, for example. I’m not talking about multiple different forms but multiple instances of the same form.
This topic has been discussed in the Nette forum so many times that it’s really a shame that it’s still missing in the documentation. Frustrated after an hour spent reading outdated forum posts, I offer here a complete solution based on one of the posts.
Form definition goes into /app/forms/ProductForm.php
-
span class=”co1″>// used in process()
-
‘title’, ‘Heading:’);
-
…
-
$this->onSuccess[] = callback($this, ‘process’‘Product saved.’
Here’s how you use it in your presenter:
-
span class=”co1″>// this needs to go into action, not render
-
‘ProductForm_’.$product[‘id’
And the final piece of the puzzle, your template:
-
New product form:
-
{form ProductForm}
-
{label title /} {input title, autofocus => TRUE}
-
{input save, value => ‘Create’}
-
<br />
-
{input body}
-
{/form}
-
-
<hr />
-
-
Existing products:
-
{foreach $products as $product}
-
{var formName = ‘ProductForm_’ . $product[‘id’]}
-
{form $formName}
-
{label title /} {input title}
-
{input save}
-
<br />
-
{input body}
-
{/form}
-
{/foreach}
-
As always, if it doesn’t work for you, check your Nette version and compare it with the tag below.
Tags: Nette Framework 2.0-beta (revision 8a3182e released on 2011-10-11)
-
-
var $parent; ?? You can always call $this->getParent() in components, or even better, you can call $this->getPresenter().
And the whole foreach thing and name composition is unnecessary. You can filter components by type http://pastebin.com/TYr9nUjB
Thanks! I really wish this stuff was in the documentation.
A note for everyone: Please don’t suggest that I add it to the docs myself. These guys have their own funny rules for what should be included and what shouldn’t.