【问题标题】:Add fileupload fields into october cms将文件上传字段添加到 october cms
【发布时间】:2019-10-16 00:38:16
【问题描述】:

我想通过添加图像字段来自定义 backend_users。我在引导方法中扩展模型和表单字段,如下所示:

public function boot()
{
    BackendUserModel::extend(function($model) {
        $model->attachOne['image'] = \System\Models\File::class;
    });

    BackendUserController::extendFormFields(function($form, $model, $context) {
        $form->addTabFields([
            'image' => [
                'label' => 'image',
                'type' => 'fileupload',
                'tab' => 'image'
            ],
       ]);
   });
}

它显示错误:

模型“System\Models\File”不包含“image”的定义。

我做错了什么?请帮我。谢谢!

【问题讨论】:

    标签: octobercms


    【解决方案1】:

    问题

    您的BackendUserController::extendFormFields 代码正在扩展您的BackendUserController 上的每个表单。

    所以要确定有两种形式

    1. 后端用户表单
    2. avatar 字段的\System\Models\File 来自

    所以,您的代码基本上是在两个表单中添加 image 字段,所以我们收到第二表单 Model 'System\Models\File' does not contain a definition for 'image'. 的错误

    解决方案

    为了避免这种情况,我们只需要通过添加condition 来确保我们是adding fieldcorrect model

    \Backend\Controllers\Users::extendFormFields(function($form, $model, $context) {
    
        // Only for the backend User model we need to add this
        if ($model instanceof \Backend\Models\User) { // <- add this condition
            $form->addTabFields([
                'image' => [
                    'label' => 'image',
                    'type' => 'fileupload',
                    'tab' => 'image'
                ],
            ]);
        }
    });
    

    现在它应该只为后端用户模型添加image 字段,您的代码应该可以正常工作。

    如有任何疑问,请发表评论。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-06-04
      • 1970-01-01
      • 2012-09-30
      • 2018-08-10
      • 2023-03-11
      • 2020-05-13
      • 1970-01-01
      • 2021-01-29
      相关资源
      最近更新 更多