CK Editor validates second time

一个人想着一个人 提交于 2019-11-28 14:53:00

You need to update instance before validation performs. So first add an id attribute or class on button like this

<input type="submit" value="Submit" id="submitFormBtn">

Now in your js code write below function

$('#submitFormBtn').click(function () {
    CKEDITOR.instances.editor1.updateElement();
    CKEDITOR.instances.editor2.updateElement();
});

Hope this will work.

jQuery.validator.setDefaults({
    ignore: [],
    // with this no hidden fields will be ignored E.g. ckEditor text-area
});

I observed the validation was working on 2nd submit. The reason is, ckEditor hides the actual text area and puts an iframe as an editor instance, and on submit it pushes the content to the text area. Which means, the validation on the TextArea gets fired on stale data. To fix this problem, I am updating my TextArea on the text change of the editor instance.

    for (instance in CKEDITOR.instances) {
        CKEDITOR.instances[instance].on('change', function ()
        {
            var editorName = $(this)[0].name;
            CKEDITOR.instances[editorName].updateElement();
        });
    }

I tried following solution and it worked .

        <textarea name="txtDesc<?php echo $i;?>" id="txtDesc<?php echo $i;?>" class="ckeditor" rows="5" cols="17" ><?php echo $txtDesc?></textarea>
        <script>
         CKEDITOR.replace("txtDesc<?php echo $i;?>");
         CKEDITOR.add       
            //CKEDITOR.replace('txtDesc0'); 
        </script>   

If there is only 1 ckEditor

for (instance in CKEDITOR.instances) {

    CKEDITOR.instances[instance].on("instanceReady", function () {

        //set keyup event
        this.document.on("keyup", function () { CKEDITOR.instances[instance].updateElement(); });
        //and paste event
        this.document.on("paste", function () { CKEDITOR.instances[instance].updateElement(); });

    });
}                                   

If there are more than 1 ckEditor(in my case 2)

CKEDITOR.instances["txtDesc0"].on("instanceReady", function () {
this.document.on("keyup", function () { CKEDITOR.instances["txtDesc0"].updateElement(); });
this.document.on("paste", function () { CKEDITOR.instances["txtDesc0"].updateElement(); });
this.document.on("cut", function () { CKEDITOR.instances["txtDesc0"].updateElement(); });
});

CKEDITOR.instances["txtDesc1"].on("instanceReady", function () {
this.document.on("keyup", function () { CKEDITOR.instances["txtDesc1"].updateElement(); });
this.document.on("paste", function () { CKEDITOR.instances["txtDesc1"].updateElement(); });
this.document.on("cut", function () { CKEDITOR.instances["txtDesc1"].updateElement(); });
});             
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!