1

我在 Yii2 项目中使用了moonlandsoft/yii2-tinymce

我根据他们的文档使用它。

use moonland\tinymce\TinyMCE;

echo TinyMCE::widget(['name' => 'text-content']);

$form->field($model, 'description')->widget(TinyMCE::className());

我不知道,他们如何首先渲染小部件,然后将模型加载到其中。

它没有价值,也没有在提交时验证。这是我表的必填字段。

控制器 :

public function actionUpdate($id) {

    $model = $this->findModel($id);

    if ($model->load(Yii::$app->request->post()) && $model->save()) {
        return $this->redirect(['view', 'id' => $model->productId]);
    } else {
        return $this->render('update', [
            'model' => $model,
        ]);
    }
}

模型 :

public function rules()
{
    return [
        [['prodname','description'], 'required'],
    ];
}

看法 :

<div class="row" style="margin-top: 10px;">
    <div class="col-md-12 col-sm-8 col-xs-12"> 
        <?php
        echo TinyMCE::widget(['name' => 'text-content']);
        $form->field($model, 'description')->widget(TinyMCE::className());
        ?>
    </div>
</div>
4

2 回答 2

0

在您的视图中,您正在显示一个不在模型中的字段 ( name),而它是 ( description) 您没有显示它。假设只description使用 TinyMCE 小部件,您的视图应如下所示:

<div class="row" style="margin-top: 10px;">
    <div class="col-md-12 col-sm-8 col-xs-12"> 

        <?= $form->field($model, 'description')->widget(TinyMCE::className()); ?>

    </div>
</div>
于 2017-04-11T09:00:03.043 回答
0

我意识到我的回复为时已晚,但仍然发布我的回复,以防像我这样的人仍然遇到问题。

对我有用的解决方案是使用triggerSave()TinyMce 提供的方法(官方链接:https ://www.tiny.cloud/docs/api/tinymce/tinymce.editormanager/#triggersave )。似乎 tinymce 编辑器默认不会将编辑器中的内容保存到原始 textarea 字段中。所以在编辑器初始化时绑定事件将内容保存到 textarea 中,如下所示:

setup: function (editor) {
    editor.on('change', function () {
        tinymce.triggerSave();
    });
}

由于 kishor10d 使用“moonlandsoft/yii2-tinymce”,因此必须自定义小部件代码以在初始化编辑器时添加上述内容。

我所做的是通过 composer 安装 tinymce,然后单独设置 TinyMceAsset.php 文件。我创建了一个完整的自定义初始化 js 调用来控制编辑器和其他功能中的选项。示例代码如下:

tinymce.init({
    selector: "#" + eleId,
    plugins: plugins,
    paste_as_text: true,
    forced_root_block: 'p',
    menubar: 'file edit view insert format tools table help',
    toolbar: 'undo redo | bold italic underline strikethrough | \n\
        fontsizeselect formatselect | \n\
        alignleft aligncenter alignright alignjustify | \n\
        outdent indent |  numlist bullist | \n\
        forecolor backcolor removeformat | pagebreak | \n\
        charmap | fullscreen  preview save print | \n\
        insertfile image media template link anchor codesample | \n\
        ltr rtl | table',
    toolbar_sticky: true,
    height: 400,
    paste_retain_style_properties: "color",
    setup: function (editor) {
        editor.on('change', function () {
            tinymce.triggerSave();
        });
    }
});
于 2020-11-07T15:11:58.107 回答