2

I'm creating a dynamic JForm in the preprocessForm function like this:

protected function preprocessForm(JForm $form, $data, $group = 'myform')
{
    ...

    foreach ($items as $i => $item) {
        $field = new \SimpleXMLElement('<field />');
        $field->addAttribute('name', '[' . $i . '][' . $item->name .']');
        $field->addAttribute('type', 'number');
        $field->addAttribute('label', JText::_('COM_MYCOMP_LABEL') . ' #' . ($i + 1));
        $form->setField($field, 'data');
        $form->setValue('[' . $i . '][' . $item->name .']', 'data', $item->value);
    }

    ...

    parent::preprocessForm($form, $data, $group);
}

The myform Form looks like this:

<?xml version="1.0" encoding="utf-8"?>
<form>
    <fieldset>  
        <field name="id" type="hidden"  label="ID" readonly="true" class="readonly"/>
        <fields name="data"></fields>
    </fieldset>
</form>   

Rendering the Field in the Template works like this:

<?php echo $this->form->getInput('[' . $i . '][fieldname1]', 'data'); ?>

Problem is I get the Field rendered like this:

<input type="number" name="jform[data][[1][fieldname1]]" id="jform_data__1__fieldname1_" value="50" class="" step="1" min="0">

But what i would like is this:

<input type="number" name="jform[data][1][fieldname1]" id="jform_data_1_fieldname1" value="50" class="" step="1" min="0">

What do i need to do, to get this working?

0

It appears to me that you are setting the '[' and ']' twice. Once in the $field->attribute(name.... and then again in the $form->setValue. If you remove one set of the square brackets then they should only appear once in the final render.

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.