如何在 fields.yaml 中添加字段但防止将它们添加到任何表单中。我只想使用“formRenderField”方法添加这些字段
问问题
1193 次
1 回答
1
我想如果您想扩展它们,那么您可以直接在“fields.yaml”文件中编写并创建它的不同版本并使用它们根据条件呈现表单
<?php namespace Acme\Blog\Controllers;
class Categories extends \Backend\Classes\Controller
{
public $implement = ['Backend.Behaviors.FormController'];
public $formConfig = 'form_config.yaml';
}
这是正常的做法
但是您可以在构造函数中添加条件来更改配置文件
public __construct() {
// if condition is true then use this config otherwise use regular one
if(condition) {
$this->formConfig = 'modified_form_config.yaml';
}
}
另一种方法是根据条件扩展表单,例如:
UsersController::extendFormFields(function($form, $model, $context){
if (!$model instanceof UserModel)
return;
$form->addFields([
'store' => [
'label'=> 'Store',
'type'=>'relation',
'nameFrom'=> 'name'
],
]);
});
您可以在插件引导方法中编写此代码
在这里,我们在调用UserController时添加字段,并且仅在它尝试呈现UserModel模型时添加。
如果您需要一些自定义场景,请描述更多,以便我们以更好的方式为您提供帮助。
于 2017-11-21T04:32:33.597 回答