Sie sind auf Seite 1von 2

JForm::setField/s - Add field to fieldset - Joomla Stack Exchange

1 of 2

http://joomla.stackexchange.com/questions/5027/jformsetfield-s-add-field...

sign up

log in tour

Joomla Stack Exchange is a question and answer site for Joomla! administrators, users, developers and designers. It's 100% free, no
registration required.

help

Sign up

JForm::setField/s - Add field to fieldset


So I'm trying to get my head around the creation of fields on the fly using
explaining how to use this method only the very minimal API description.

JForm::setField

I want to add a number of fields to a fieldset in a form on the fly. I've figured out that
what it is supposed to do...?

. But as usual there is no documentation

$group

is not the fieldset name so I don't understand

I've successfully added the field using the following:


$element=newSimpleXMLElement('<fieldname="onfly"
type="text"
label="onfly"
description="onflydesc"
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

Also, since I will be using a loop to add multiple fields, should I be using
use that method!

$element

but that deletes any preexisting fields in that fieldset.

JForm::setFields

instead? In which case I haven't a clue how to

Any help would be appreciated! Thanks


jform

asked Oct 31 '14 at 7:14


doovers
279

12

3 Answers

I am not sure, but you can try

load

method:

$element='
<fieldname="onfly"
type="text"
label="onfly"
description="onflydesc"
class="inputbox"
size="30"
required="true"/>
';
//Defineyourfieldsethere
$xpath='//fieldset[@name="yourfieldset"]';
JForm>load($element,false,$xpath);

answered Oct 31 '14 at 9:08


Dmitry Rekun
3,062

32

Unfortunately this didn't work but thanks for the idea! doovers Nov 4 '14 at 6:23

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($newfieldsas$field){
$elements[]=newSimpleXMLElement('<fieldname="onfly_'.$field.'"
type="text"
label="onfly_'.$field.'"

11/13/2015 5:41 PM

JForm::setField/s - Add field to fieldset - Joomla Stack Exchange

2 of 2

http://joomla.stackexchange.com/questions/5027/jformsetfield-s-add-field...

description="onflydesc"
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);
}

answered Dec 9 '14 at 4:49


doovers
279

12

Seems you can not add any new field in any existing fieldset. You need to create a new fieldset
in the following format
$element=newSimpleXMLElement('<fieldsetname="any_name">
<fieldname="onfly"
type="text"
label="onfly"
description="onflydesc"
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.
answered Dec 9 '14 at 5:10
Gaurav
347

11/13/2015 5:41 PM

Das könnte Ihnen auch gefallen