Skip to main content
edited tags
Link
mickmackusa
  • 4.8k
  • 4
  • 18
  • 48
Source Link
Mike
  • 351
  • 2
  • 11

Creating a multidimensional JForm field dynamicaly

Item Model

protected function preprocessForm(JForm $form, $data, $group = 'examplegroup')
{
    foreach ($data as $i => $item) {
        $field = new \SimpleXMLElement('<field />');
        $field->addAttribute('name', 'sample[' . $i . ']');
        $field->addAttribute('class', 'input-mini');
        $field->addAttribute('type', 'number');
        $form->setField($field, 'data');
        $form->setValue('sample[' . $i . ']', 'examplegroup', $item->fieldname);
        ...
    }
    ...
}

Layout

<?php foreach ($this->items->data as $i => $item) : ?>
<?php echo $this->form->getInput('sample[' . $i . ], 'examplegroup'); ?>

Output

<input type="number" name="jform[examplegroup][sample[0]]" value="6" class="input-mini">

How can I achieve getting this kind of name name="jform[examplegroup][sample][0]" ? The form needs to be generated dynamicaly and not by writing the name static into the code.