生成要在"管理"视图中显示的动态表单



我在一个组件上,我希望用户将自己的字段添加为另一条记录的选项。

例如,我有一个视图产品(管理员/组件/com_myproducts/视图/产品/...(。此视图将获取其表单的标题、别名和描述(管理员/组件/com_myproducts/模型/表单/产品.xml(。xml表单是静态的,但我希望用户自己添加产品属性。

所以我正在添加另一个视图属性,用户可以在其中添加具有名称和字段类型的记录。

现在,我希望将这些属性添加到"产品"窗体中。因此,它基本上是从数据库中获取属性,加载表单XML,将属性附加到XML并将修改后的XML提供给JForm对象以将其返回到产品模型getForm((中。

在产品模型中,这看起来像这样:


public function getForm($data = array(), $loadData = true)
{
// Get the form.
$form = $this->loadForm(
'com_myproducts.product',
'product',
array(
'control' => 'jform',
'load_data' => $loadData
)
);
$xml = $form->getXML();
$attributes = $this->getAttributes();
$newForm = Helper::manipulateFormXML($xml, $attributes);
/*
* load manipulated xml into form
* don't know what to do here
*/
...
if (empty($form)) {
return false;
}
return $form;
}

如何使用修改后的 xml 更新表单,还是应该以另一种方式处理?

我通过创建表单的新实例找到了解决问题的方法。

public function getForm($data = array(), $loadData = true)
{
// Get the form.
$tmp = $this->loadForm(
'com_myproducts.tmp',
'product',
array(
'control' => 'jform',
'load_data' => $loadData
)
);
$xml = $tmp->getXML();
$attributes = $this->getAttributes();
$newXml = Helper::manipulateFormXML($xml, $attributes);
// Get the form.
$form = $this->loadForm(
'com_myproducts.product',
$newXml->asXML(),
array(
'control' => 'jform',
'load_data' => $loadData
)
);
if (empty($form)) {
return false;
}
return $form;
}

我对这个解决方案不满意,但它可以满足我的需求。

最新更新