How to name field as array of values

+
0
-

How do I set multiple fields as an array?

When I do this:

<textarea n:name=method[] size=20></textarea>

An error occurs:

Nette\InvalidArgumentException
Component name must be non-empty alphanumeric string, 'method[]' given.

How can I have multiple fields, named as an array?

Kamil Valenta
Member | 762
+
+1
-
$method = $form->addContainer('method');
$method->addTextarea(1);
$method->addTextarea(2);
...
$method->addTextarea(n);
+
0
-

Thanks Kamil,
I can create the array of values in the presenter:

<?php

$container = $form->addContainer('items');
$container->addText('1', 'Item 1:');
$container->addText('2', 'Item 2:');
$container->addText('3', 'Item 3:');
?>

but how do I access $container from the template when creating the form?

kamil_v wrote:

$method = $form->addContainer('method');
$method->addTextarea(1);
$method->addTextarea(2);
...
$method->addTextarea(n);
David Grudl
Nette Core | 8133
+
0
-

I think this should work

<textarea n:name=method-0 size=20></textarea>
<textarea n:name=method-1 size=20></textarea>
<textarea n:name=method-2 size=20></textarea>
+
0
-

David Grudl wrote:

I think this should work

<textarea n:name=method-0 size=20></textarea>
<textarea n:name=method-1 size=20></textarea>
<textarea n:name=method-2 size=20></textarea>

Thanks David,
This produces an error I have not seen previously.

TypeError
levenshtein() expects parameter 1 to be string, integer given
David Grudl
Nette Core | 8133
+
0
-

You found a bug.

Because there is $container->addText('1', 'Item 1:');, in template it should be <textarea n:name=item-1></textarea> instead of <textarea n:name=item-0></textarea>.

But I don't know exactly what you're trying to achieve, maybe it could be easier.

+
0
-

David Grudl wrote:

You found a bug.

Because there is $container->addText('1', 'Item 1:');, in template it should be <textarea n:name=item-1></textarea> instead of <textarea n:name=item-0></textarea>.

But I don't know exactly what you're trying to achieve, maybe it could be easier.

So, my goal is to produce a text area with

<textarea name="tags[]"></textarea>

The solution you propose almost gets there. The presenter as the following

<?php
$container = $form->addContainer('tags');
$container->addText('1', 'Tag 1:');
$container->addText('2', 'Tag 2:');
$container->addText('3', 'Tag 3:');
?>

And, as you rightly pointed out, to access this in the template..

<textarea n:name=tags-1></textarea>
<textarea n:name=tags-2></textarea>
<textarea n:name=tags-3></textarea>

However, this produces textarea fields with:

<textarea name="tags[1]" ...
<textarea name="tags[2]" ...
<textarea name="tags[3]" ...

I need to have the index blank, for the element to be cloned with javascript.

<textarea name="tags[]" ...
<textarea name="tags[]" ...
<textarea name="tags[]" ...
David Grudl
Nette Core | 8133
+
+2
-

If the number of elements is arbitrary, I wouldn't create any elements on the PHP side and on the side of the template I would use your original code:

<textarea name="tags[]" ...
<textarea name="tags[]" ...
<textarea name="tags[]" ...

No n:name because there are no counterparts in the form.

Once sent, the values stored in the text fields can be obtained as follows:

$tags = $form->getHttpData($form::DATA_TEXT, 'tags[]');  // or DATA_LINE for <input>

where 'tags[]' is the same as attribute name in template.