4

So I'm trying to get my head around the creation of fields on the fly using JForm::setField. But as usual there is no documentation explaining how to use this method only the very minimal API description.

I want to add a number of fields to a fieldset in a form on the fly. I've figured out that $group is not the fieldset name so I don't understand what it is supposed to do...?

I've successfully added the field using the following:

$element = new SimpleXMLElement('<field name="onfly"
                                        type="text"
                                        label="onfly"
                                        description="onfly desc"
                                        class="inputbox"
                                        size="30"
                                        required="true" />');
$form->setField($element);

But I can't add it to the fieldset I want. I can add the <fieldset> tags to $element but that deletes any preexisting fields in that fieldset.

Also, since I will be using a loop to add multiple fields, should I be using JForm::setFields instead? In which case I haven't a clue how to use that method!

Any help would be appreciated! Thanks

2

I am not sure, but you can try load method:

$element = '
            <field name="onfly"
            type="text"
            label="onfly"
            description="onfly desc"
            class="inputbox"
            size="30"
            required="true" />
            ';
// Define your fieldset here
$xpath = '//fieldset[@name="yourfieldset"]';

JForm->load($element, false, $xpath);
  • Unfortunately this didn't work but thanks for the idea! – doovers Nov 4 '14 at 6:23
1

The solution I used here was to forget about trying to add the field to a preexisting fieldset and add multiple fields to a group instead.

$newfields = array(1, 2, 3, 4, 5)
$elements = array();
foreach ($newfields as $field) {
    $elements[] = new SimpleXMLElement('<field name="onfly_'.$field.'"
                                                type="text"
                                                label="onfly_'.$field.'"
                                                description="onfly desc"
                                                class="inputbox"
                                                size="30"
                                                required="true" />');
}
$form->setFields($elements, 'newGroup');

This allowed me to loop through each field in the group to render them all

foreach ($this->form->getGroup('newGroup') as $field) { 
    echo $field->renderField($options);
}
0

Seems you can not add any new field in any existing fieldset. You need to create a new fieldset in the following format

$element = new SimpleXMLElement('<fieldset name="any_name">
                                    <field name="onfly"
                                          type="text"
                                          label="onfly"
                                          description="onfly desc"
                                          class="inputbox"
                                          size="30"
                                          required="true" />
                                  </fieldset>');
$form->setField($element);

You can multiple field tag in the same fieldset tag.

Another alternate way could be (I am not sure) : You can read the xml file, find the fieldset and add children to it. After that, you can load the new xml string into same $form instance.

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.