php - 生成动态表单以显示在管理视图中
问题描述
我在一个组件上,我希望用户将自己的字段添加为另一条记录的选项。
例如,我有一个视图产品 (administrator/components/com_myproducts/views/product/...)。此视图将获得其标题、别名和表单描述 (administrator/components/com_myproducts/models/forms/product.xml)。表单 xml 是静态的,但我希望用户自己添加产品属性。
所以我添加了另一个视图属性,用户可以在其中添加带有名称和字段类型的记录。
现在我希望将这些属性添加到产品表单中。所以它基本上是从数据库中获取属性,加载表单 XML,将属性附加到 XML 并将修改后的 XML 提供给 JForm 对象以在产品模型 getForm() 中返回它。
在 Product 模型中,这看起来像这样:
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;
}
我对这个解决方案不满意,但它可以满足我的需求。
推荐阅读
- php - 需要帮助以正确编写代码以创建帖子的“loadmore”按钮和“like”按钮
- javascript - 使用承诺链接方法时如何确保变量不为空?
- c++ - 不推荐使用有关选项“Gm”的不相关警告
- javascript - 如何隐藏背景 HDR 贴图的显示,但在 three.js 中保存 hdri 的照明工作?
- python - 有没有办法访问精灵组中的元素?
- python - 为序列-InvalidArgument 构建/训练 1D CNN
- docker - 通过 Docker compose 文件配置本地代理?
- r - 在 R 模拟中估计线性回归和线性混合模型中的偏差
- python - NameError:加载 keras 模型时未定义名称“feature_extractor_url”
- bash - 通过 ssh 运行多个 rsync 命令并且只输入一次 ssh 密码