我创建了一个与另一个模型有关系的模型。我使用关系管理器小部件来处理关系。一切都很好。
现在,我需要根据父模型中字段的选择动态地向关系模型添加字段。我注意到有一个relationExtendManageWidget()函数可以实现这一点。
但是,我不知道如何使用它,文档中也没有提供任何示例。我试过这样做:
public function relationExtendManageWidget($widget, $field, $model)
{
// Make sure the field is the expected one
if ($field != 'references') return;
$widget->addFields([
'test' => [
'label' => 'Test',
'span' => 'left',
'type' => 'text',
],
]);
return $widget;
}
但是当我转到我的表单时,我得到了以下错误:
在null /modules/backend/widgets/Form.php行569上对成员函数addField()的
调用
发布于 2018-06-22 07:35:25
$widget对象(Widget Base documentation)上不存在addFields
方法。为了实现您想要做的事情,我建议使用extendFormFields方法来扩展表单域,或者创建一个分部。使用extendFormFields的示例如下所示(代码未经测试):
public function extendFormFields(function($form, $model, $context)
{
if (!$model instanceof MyModel) { //your related model
return;
}
$form->addFields([
'test' => [
'label' => 'Test',
'span' => 'left',
'type' => 'text',
],
]);
});
发布于 2019-01-21 07:59:14
将字段配置直接添加到$widget->fields
,而不是使用$widget->addFields(...)
。这对我很有效。
public function relationExtendManageWidget($widget, $field, $model)
{
// Make sure the field is the expected one
if ( $field != 'references') return;
$widget->fields += [
'test' => [
'label' => 'Test',
'span' => 'left',
'type' => 'text',
],
];
}
发布于 2019-04-26 15:42:05
对于manage小部件,没有明确的文档说明,但它与Extending the view widget下的文档中的说明相同
widget由于小部件在运行时周期的此时尚未完成初始化,因此不能调用$
->removeColumn()。ListController文档中描述的addColumns()方法将按预期工作,但要删除列,我们需要侦听relationExtendViewWidget()方法中的'list.extendColumns‘事件。
为了添加字段,我们需要监听form.extendFields事件:
public function relationExtendManageWidget($widget, $field, $model)
{
if ($field != 'references') return;
$widget->bindEvent('form.extendFields', function () use($widget) {
$widget->addFields([
'test' => [
'label' => 'test'
],
]);
});
}
https://stackoverflow.com/questions/50947608
复制相似问题