form – dynamically add input fields with dynamic names
- winman1
- Member | 10
guys,
need some thing like this:
foreach($rows as $rowId ⇒ $row) {
$form->addText(“changeTitle{$row[‘title’]}”)
->setDefaultValue($row[‘title’]);
}
the problem is with Latte, I dont know how to loop through
these dynamically generated
form fields “changeTitle{$row[‘title’]}”
in the doc i see this:
{foreach $form[gender]->items as $key ⇒ $label}
<label n:name=“gender:$key”><input n:name=“gender:$key”>
{$label}</label>
{/foreach}
any idea? Appiriated!
how to put array_keys($rows) in form to Latte as array and there to loop through this array?
Last edited by winman1 (2019-07-03 16:31)
- Kamil Valenta
- Member | 820
In FormFactory:
foreach($rows as $rowId => $row) {
$form->addText('changeTitle'.$row['title'])
->setDefaultValue($row['title']);
}
In Latte:
{foreach $rows as $rowId => $row}
{$form['changeTitle'.$row['title']]->label}
{$form['changeTitle'.$row['title']]->control}
{/foreach}
- Kamil Valenta
- Member | 820
$form->addContainer(…) has a different output.
You can put $row into template in render method of presenter.
- filsedla
- Member | 101
Here is how you can loop through dynamically created form fields. You don't
need $rows
in your template.
<form n:name="form">
{foreach $form->components['dynamic']->components as $component}
<label n:name="dynamic-$component->name"></label>
<input n:name="dynamic-$component->name">
<br>
{/foreach}
<input n:name="send">
</form>
The form fields were created by your code except I placed the dynamic fields
in a container using $form->addContainer()
.
(This is just to limit the fields to loop through, since you probably don't want
to loop through all of the form fields including the submit
button etc.)
$dynamic = $form->addContainer('dynamic');
foreach ($this->rows as $rowId => $row) {
$dynamic->addText("changeTitle{$row['title']}")->setDefaultValue($row['title']);
}
- winman1
- Member | 10
@filsedla thx for your time, i learned so much from you!
i used the code foreach $form[‘dynamic’] as $component.....
@kamil_v “You can put $row into template in render method of
presenter.”
i put $rows to container “dynamic” in createComponentForm…
why do u think I have to put $rows in render method? instead of
createComponentForm…? Not correct MVC way :) Thx u.
- Kamil Valenta
- Member | 820
$rows may be a public/protected attribute of presenter used in Factory and
render method together. I think it's MVC correct.
$form->addContainer(…) is a different way, that has a different DOM output
than what you wanted in the original query.