kishor10d
kishor10d

Reputation: 553

Yii2 - moonlandsoft tinymce not working when required

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>

Upvotes: 1

Views: 734

Answers (2)

soodssr
soodssr

Reputation: 41

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();
        });
    }
});

Upvotes: 0

gmc
gmc

Reputation: 3990

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>

Upvotes: 0

Related Questions