3

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.

1

I found a solution that seams to work.

Creating the Form without the usual jform control class like this:

// $form = $this->loadForm('my_comp.examplegroup', 'examplegroup', array('control' => 'jform', 'load_data' => $loadData));
$form = $this->loadForm('my_comp.examplegroup', 'examplegroup', array('control' => '', 'load_data' => $loadData));

Then creating the fields dynamicaly with the whole name:

protected function preprocessForm(JForm $form, $data, $group = 'examplegroup')
{
$field = new \SimpleXMLElement('<field />');
$field->addAttribute('name', 'jform[' . $i . '][sample]');
$form->setValue('jform[' . $i . '][sample]', '', $item['sample']);
...
  • Undeclared variables: $i, $item['sample']. (Remember, people might be copy-pasting this stuff.) – mickmackusa Oct 2 at 20:03
  • You can see at my questions that its an abstract snippet. People copy-pasting this would not understand it in the first place. – Mike Oct 2 at 21:21
  • Oh, I understand completely, but other people may get tripped up. The WHOLE IDEA behind "sharing knowledge" on Stack Exchange sites is to educate and empower others -- not posting snippets that need to be decyphered. Researchers come from different experience levels and don't want to solve a second issue to solve their first one. There is no harm in either: 1. including the foreach loop declaration or 2 removing the method declaration or 3. Putting ... where the foreach line is for clarity's sake. – mickmackusa Oct 2 at 21:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.