1

I am using moonlandsoft/yii2-tinymce in my Yii2 project.

I am using it according to their documentation.

use moonland\tinymce\TinyMCE;

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

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

I don't know, how they are first rendering the widget and then loading the model into it.

It is not taking value and not validating on submit. It is required field of my table.

Controller :

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,
        ]);
    }
}

Model :

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

View :

<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>
kishor10d
  • 543
  • 6
  • 24

2 Answers2

0

In your view, you are displaying a field that is not in your model (name), and the one it is (description) you are not displaying it. Assuming only description will use the TinyMCE widget, your view should look like this:

<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>
gmc
  • 3,910
  • 2
  • 31
  • 44
  • Actually I pasted only necessary code. ```prodname``` is there in my code. – kishor10d Apr 11 '17 at 09:01
  • I tried this, it is ```2amigos/yii2-tinymce-widget```. But I am using ```moonlandsoft/yii2-tinymce```. – kishor10d Apr 11 '17 at 09:03
  • Oh, I see. So what happens exactly? It displays the field? It show some error? The result is different when you echo than when you don't? – gmc Apr 11 '17 at 09:10
  • Actually, it loads the content in editor, but when I clicked on save button, it never saved. Validation also not working, page redirects to blank. – kishor10d Apr 11 '17 at 09:19
  • That is another problem, you are using `redirect` like it was `render`. Instead of redirect use `return $this->goBack()`. I wold `var_dump` the content of `$model` after loading POST – gmc Apr 11 '17 at 09:26
  • actually redirection is not a problem. Redirects to blank mean when the form submits, it submits to ```index.php?r=product/update&id=10``` where id is ```productId```, then product is not get saved. And just because ```description``` is blank, it is not saving and shows blank page with same url. – kishor10d Apr 11 '17 at 09:33
0

I realize that my reply is too late, but still posting my reply in case any one like me still encountered the issue.

Solution that worked for me is to use triggerSave() method provided by TinyMce (Official link: https://www.tiny.cloud/docs/api/tinymce/tinymce.editormanager/#triggersave). It seems that the tinymce editor does not save the content from the editor into the original textarea field by default. So bind the event to save the content into textarea when editor is initialized as follows:

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

As kishor10d is using "moonlandsoft/yii2-tinymce" then the widget code has to be customized to enable adding the above at the time of initializing the editor.

What I did was to install tinymce via composer instead and then set TinyMceAsset.php file separately. And I created an entire custom initialization js call to control options in editor and other functionality. Sample code is as follows:

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();
        });
    }
});
soodssr
  • 41
  • 1
  • 6