Php Yii2-需要时,moonlandsoft tinymce不工作

Php Yii2-需要时,moonlandsoft tinymce不工作,php,yii2,tinymce,yii2-widget,Php,Yii2,Tinymce,Yii2 Widget,我正在我的Yii2项目中使用 我根据他们的文档使用它 use moonland\tinymce\TinyMCE; echo TinyMCE::widget(['name' => 'text-content']); $form->field($model, 'description')->widget(TinyMCE::className()); 我不知道,他们是如何首先呈现小部件,然后将模型加载到其中的 它没有获取值,也没有在提交时进行验证。它是我表格的必填字段 控制器:

我正在我的Yii2项目中使用

我根据他们的文档使用它

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>

在您的视图中,您正在显示一个不在您的模型中的字段(
名称
),而它所在的字段(
说明
)则不在显示。假设只有
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>

在您的视图中,您正在显示一个不在您的模型中的字段(
名称
),而它所在的字段(
说明
)则不在显示。假设只有
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>

我意识到我的回复太晚了,但我还是会发布我的回复,以防像我这样的人仍然遇到这个问题

对我有效的解决方案是使用TinyMce提供的
triggerSave()
方法(官方链接:)。默认情况下,tinymce编辑器似乎不会将编辑器中的内容保存到原始textarea字段中。因此,当编辑器初始化时,绑定事件以将内容保存到textarea,如下所示:

setup: function (editor) {
    editor.on('change', function () {
        tinymce.triggerSave();
    });
}
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();
        });
    }
});
由于kishor10d正在使用“moonlandsoft/yii2 tinymce”,因此必须对小部件代码进行自定义,以便在初始化编辑器时添加上述内容

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

setup: function (editor) {
    editor.on('change', function () {
        tinymce.triggerSave();
    });
}
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();
        });
    }
});

我意识到我的回复已经太晚了,但我仍然会发布我的回复,以防像我这样的人仍然遇到这个问题

对我有效的解决方案是使用TinyMce提供的
triggerSave()
方法(官方链接:)。默认情况下,tinymce编辑器似乎不会将编辑器中的内容保存到原始textarea字段中。因此,当编辑器初始化时,绑定事件以将内容保存到textarea,如下所示:

setup: function (editor) {
    editor.on('change', function () {
        tinymce.triggerSave();
    });
}
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();
        });
    }
});
由于kishor10d正在使用“moonlandsoft/yii2 tinymce”,因此必须对小部件代码进行自定义,以便在初始化编辑器时添加上述内容

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

setup: function (editor) {
    editor.on('change', function () {
        tinymce.triggerSave();
    });
}
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();
        });
    }
});

发布您的代码。@gmc:我添加了我的代码。请看一看。发布您的代码。@gmc:我添加了我的代码。请看一下。实际上我只粘贴了必要的代码<代码>prodname在我的代码中。我尝试了这个,它是
2amigos/yii2 tinymce小部件
。但是我用的是
moonlandsoft/yii2 tinymce
。哦,我明白了。那么到底发生了什么?它显示字段?它显示出一些错误吗?回显时的结果与不回显时的结果不同?实际上,它在编辑器中加载内容,但当我单击“保存”按钮时,它从未保存。验证也不起作用,页面重定向到空白。这是另一个问题,您正在使用
重定向
,就像使用
渲染
一样。使用
返回$this->goBack()
代替重定向。我将
var_dump
加载后粘贴
$model
的内容,实际上我只粘贴了必要的代码<代码>prodname在我的代码中。我尝试了这个,它是
2amigos/yii2 tinymce小部件
。但是我用的是
moonlandsoft/yii2 tinymce
。哦,我明白了。那么到底发生了什么?它显示字段?它显示出一些错误吗?回显时的结果与不回显时的结果不同?实际上,它在编辑器中加载内容,但当我单击“保存”按钮时,它从未保存。验证也不起作用,页面重定向到空白。这是另一个问题,您正在使用
重定向
,就像使用
渲染
一样。使用
返回$this->goBack()
代替重定向。加载POST后,我将查看
var\u dump
$model
内容