How to manually render an array of text inputs?

3 years ago

netteman
Member | 78
+
0
-

Hi, I have this method

public function createComponentProductForm() {

    $form = new Form();

    $arr = [1 => "Product A", 2 => "Product B", 3 => "Product C"];

    $sub1 = $form->addContainer("product");

    foreach($arr as $id => $name){
        $sub1->addText($id, $name)
                ->setRequired()
                ->setDefaultValue(0);
    }//end foreach

    $form->addSubmit("send", "send");

    $form->onSuccess[] = [$this, 'productFormSucceeded'];

    return $form;

}//end mtd

And I would like to render the form manually (because I want to add some html code right before the <input> part).

I don't know how to access the array/container product[] in latte so I can use something like

{form productForm}
{foreach $iDontKnowWhatToPutHere as $input}
    {label $input}<div>My html</div>{input $input}
{/foreach}
{/form}

When I {dump $form->components} I can see

Nette\ComponentModel\RecursiveComponentIterator #d8d3
product => Nette\Forms\Container #88f6
onValidate => NULL
currentGroup protected => NULL
validated private => NULL
components private => array (3)
1 => Nette\Forms\Controls\TextInput #aa24 { ... }
2 => Nette\Forms\Controls\TextInput #70c3 { ... }
3 => Nette\Forms\Controls\TextInput #a1a8 { ... }
cloning private => NULL
parent private => Nette\Application\UI\Form #d455
name private => "product" (7)
monitors private => array ()
send => Nette\Forms\Controls\SubmitButton #c3f5
_do => Nette\Forms\Controls\HiddenField #dbf7

But I don't know how to access the text inputs.

Thanks :)

3 years ago

CZechBoY
Member | 3407
+
0
-

for example

{form producForm}
<table>
<tr n:foreach="$form->controls as $input" n:class="$input->required ? required">
<th>{label $input /}</th>
<td>{input $input} <span class=error n:ifcontent>{$input->error}</span></td>
</tr>
</table>
{/form}

http://rjwebdesign.cz/…tipy-k-nette

Last edited by CZechBoY (2016-11-12 16:31)

3 years ago

netteman
Member | 78
+
0
-

Thank you!

Here's the code that does what I need

public function createComponentProductForm() {

    $form = new Form();

    $arr = [1 => "Product A", 2 => "Product B", 3 => "Product C"];

    $sub1 = $form->addContainer("product");

    foreach($arr as $id => $name){
        $sub1->addText($id, $name)
                ->setRequired()
                ->setDefaultValue(0);
    }//end foreach

    $form->addSubmit("send", "send");

    $form->onSuccess[] = [$this, 'productFormSucceeded'];

    return $form;

}//end mtd
{form productForm}

    {foreach $form[product]->components as $input}
        {label $input} - <span>My html</span> - {input $input} <br />
    {/foreach}

    {input send}

{/form}