我正在使用jForm
和方法setField()
以及getFieldset()
设置和检索字段,但是Joomla引发了错误。在尝试添加list
Joomla 之前,我无法解析XML,直到我添加了option_on="Yes"
和option_off="Yes"
。(我不确定这些是什么/为什么是必要的,但是使用它们,下面的代码可以工作)。
models \ myform.php
public function getForm($data = array(), $loadData = true)
{
// Get the form.
$form = $this->loadForm('com_mycomponent.mymodel', 'myform', array('control' => 'jform', 'load_data' => $loadData));
if (empty($form)) {
return false;
}
$element = new SimpleXMLElement('<fieldset name="myFieldset">
<field name="myfield1" type="list"
label="My List"
default="2"
option_on="Yes"
option_off="Yes">
<option value="1">Low</option>
<option value="2">Normal</option>
<option value="3">High</option>
</field>
<field name="myfield2" type="text" label="My field 1" class="inputbox" size="30" />
<field name="myfield3" type="text" label="My field 2" class="inputbox" size="30" />
</fieldset>');
$form->setField($element);
return $form;
}
views / myview / tmpl / default.php
$this->form->getFieldset('myFieldset'), true)
// Loop through these results and display them accordingly
$myFieldset = $this->form->getFieldset('myFieldset');
if(count($myFieldset)){
foreach($myFieldset as $field) {
$field_name = $field->getAttribute('name');
echo $this->form->getLabel($field_name);
echo $this->form->getInput($field_name);
}
}
控制器/ myview.php
// Get the user data.
$data = JFactory::getApplication()->input->get('jform', array(), 'array');
/* $data DOES contain my input fields*/
// The model/table doesn't contain columns for my custom fields, so the data to be saved has to be manipulated here to "go somewhere permanent".
// Validate the posted data.
$form = $model->getForm();
/* $form DOES contain my input fields*/