2

I'm using CKeditor and the jQuery validation plugin from basistance. My textarea (with the CKEditor on it) is being validated by jQuery, but that only works after the second click on my submit button.

In short: the first time I submit the form when data is entered in the CKEditor, it says "field is empty". The second time it says it's ok and the form is being submitted.

I read a solution for this:

"you could work around this problem by calling CKEDITOR.editor::updateElement right before every validation routine."

I cannot find how to implement it though:

$(document).ready(function(){
    CKEDITOR.replace( 'prod_description',
    {
        toolbar: 'MyToolbar'
    }
    );

    $("#btnOk").click(function(){
        CKEDITOR.instances.editor1.updateElement();
        alert('click');
    });
});

This always gives me the error: "CKEDITOR.instances.editor1 is undefined"

Any ideas on how to solve this. Documentation from CKEditor is here: http://docs.cksource.com/ckeditor_api/symbols/CKEDITOR.editor.html#updateElement

Jorre
  • 17,273
  • 32
  • 100
  • 145
  • 1
    console.dir(CKEDITOR); console.dir(CDEDITOR.instances); – czarchaic Dec 14 '09 at 21:10
  • is that java? I'm using Javascript and Jquery in an html-page – Jorre Dec 14 '09 at 21:12
  • no its javascript. Are you using firebug? – czarchaic Dec 14 '09 at 21:36
  • Yes I'm using firebug. console.dir(CKEDITOR.intances); -> prints nothing in the console, but CKEditor is showing up just fine... – Jorre Dec 14 '09 at 21:46
  • I'm getting close thanks to you czarchaic! CKEDITOR.instances.prod_description.updateElement(); That's what I need, all I need right now is a good place to call this update. It should be a pre-validate location in my code, right before the jquery validation plugin kick in. Any ideas on that one? – Jorre Dec 14 '09 at 21:52

3 Answers3

4

I solved it by writing:

CKEDITOR.instances.prod_description.updateElement();

where "prod_description" is the name of your textarea with CKeditor linked to it.

Zoe
  • 27,060
  • 21
  • 118
  • 148
Jorre
  • 17,273
  • 32
  • 100
  • 145
1

This may not be the best way to do it, but I created a jquery rule that simply checks to see if there is data in the CKEditor. If there is data, then it passes the rule. If not, then it fails. This seems to work for me pretty well.

//Have to build custom method to check ckeditor
jQuery.validator.addMethod("ckeditor", function(value, element) { 
    var textData = editor.getData();
    if(textData.length>0) return true;
    return false;
}, "No data in editor");

Then, my rule looks like this:

'fieldName': "ckeditor"
Scott Harwell
  • 7,457
  • 2
  • 28
  • 41
1

I have multiple CKEDITORS on my page, so I do this:

        // You need to update the editors before jqValidator.
        for (var i in CKEDITOR.instances)
        {
            CKEDITOR.instances[i].updateElement();
        }
        if (!jqValidator.form())
        {
            alert('Error Alert: please correct the errors detailed below, then click "Apply changes" again.');
            return;
        }
Paul Tomblin
  • 179,021
  • 58
  • 319
  • 408