zendframework / zend-form

Form component from Zend Framework
BSD 3-Clause "New" or "Revised" License
69 stars 87 forks source link

Square brackets in name of form element #107

Closed albrin closed 7 years ago

albrin commented 8 years ago

How can I use square brackets in name of form element?

$element = new Element\Text();
$element->setName('type1[FIO]');
// ...
$personalType1->add($element);

Render form

...
<input type="text" value="" placeholder="Ф.И.О." id="personalType1FIO" class="form-control order-group__input" name="type1[FIO]">
...

$_POST ($request->getPost()->toArray())

Array
(
    [type1] => Array
        (
            [FIO] => My FIO
        )
)

But when I do fill and submit the form - not the values are set and the form does not pass validation.

$personalType1->setData($request->getPost());
robertmarsal commented 8 years ago

You need to use Collections for this functionality. type1 becomes your collection and FIO is an item in this collection

weierophinney commented 8 years ago

You can accomplish this with fieldsets. When you add a fieldset to a form (or even another fieldset), it gets nested in the way you indicated, causing array notation in the client.

froschdesign commented 7 years ago

Closing because question was answered.

stephaneeybert commented 7 years ago

What about the ajax route ? Any way ?

This array item: 'clients[0]': $('[name="clients[0]-' + id + '"]').val() gets its closing square bracket stripped away.

froschdesign commented 7 years ago

@stephaneeybert Sorry, but I think this is the wrong place for question. There is no Ajax or JavaScript.