Documentos de Académico
Documentos de Profesional
Documentos de Cultura
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
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...?
$group
<fieldset>
tags to
Also, since I will be using a loop to add multiple fields, should I be using
use that method!
$element
JForm::setFields
12
3 Answers
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);
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
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);
}
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);
field
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