我在我的Yii2项目中使用moonlandsoft/yii2-tinymce。
我正在根据他们的文档使用它。
use moonlandtinymceTinyMCE;
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>
在您看来,您正在显示一个不在模型中的字段 ( 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>
我意识到我的回复为时已晚,但仍然发布我的回复,以防像我这样的人仍然遇到问题。
对我有用的解决方案是使用TinyMce提供triggerSave()
方法(官方链接:https://www.tiny.cloud/docs/api/tinymce/tinymce.editormanager/#triggersave(。似乎默认情况下,tinymce编辑器不会将编辑器中的内容保存到原始文本区域字段中。因此,绑定事件以在初始化编辑器时将内容保存到文本区域中,如下所示:
setup: function (editor) {
editor.on('change', function () {
tinymce.triggerSave();
});
}
由于kishor10d使用的是"moonlandsoft/yii2-tinymce",因此必须自定义小部件代码,以便在初始化编辑器时添加上述内容。
我所做的是通过作曲家安装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();
});
}
});