Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get TinyMCE and Jquery validate to work together?

I am using jquery validate and the jquery version of tinymce.

I found this piece of code that makes tinymce to validate itself every time something changes in it.

Hi

I am using the jquery validate with my jquery tinymce so I have this in my code

    // update validation status on change
    onchange_callback: function (editor)
    {
        tinyMCE.triggerSave();
        $("#" + editor.id).valid();
    },

This works however there is one problem. If a user copies something from word it brings all that junk styling with it what is usually over 50,000 characters. This is way over my amount of characters a user is allowed to type in.

So my jquery validation method goes off telling me that they went over the limit. In the mean time though tinymce has cleaned up that mess and it could be possible now the user has not gone over the limit.

Yet the message is still there.

So is there a better function call I can put this in? Maybe tell tinymce to delay the valid when a paste is happening, or maybe a different callback?

Anyone got any ideas?

like image 441
chobo2 Avatar asked May 29 '10 05:05

chobo2


People also ask

Does Tinymce need jQuery?

You don't need to use tinymce as a jQuery plugin but the option is there if you would like to. The vast majority of the tinymce source code is in the tinymce. min. js file and the jQuery.

What is Tinymce jQuery?

The TinyMCE for jQuery plugin also adds a tinymce pseudo selector. This can be used to get all the instances of a page or check if an element has an editor instance or not. Below is an example on how to use the pseudo selector. // Getting all converted textareas and set contents to the $('textarea:tinymce').

How do you destroy Tinymce?

Use tinymce. remove() method to remove TinyMCE editor from the HTML element and again call tinymce. init() on the selector to reinitialize.


2 Answers

After hours of fiddling out how to get validation working with a TinyMCE editor i finally found that it is necessary to tell the validation plugin to NOT ignore hidden fields (as the original textarea is hidden when tinyMCE is initialised). So the solution in short is to:

$.validator.setDefaults({
    ignore: ''
});

This code tells the jquery Validation Plugin to check hidden fields as well... And then you can normally...

 onchange_callback: function (editor)
{
    tinyMCE.triggerSave();
    $("#" + editor.id).valid();
},

This isn't a direct answer to the original question - i know. But since this page is #1 by google for "jquery validate tinymce" i think it's a good place to mention it here.

danludwig mentioned this here: Unable to get TinyMCE working with jQuery Unobtrusive Validation

like image 108
weekender Avatar answered Oct 14 '22 10:10

weekender


Oh yeah, I also faced this problem.

So, I fixed it by calling the validation on the click event of a button instead.

$("#buttontosave").click(function() {
         tinyMCE.triggerSave();
         var status;
         status = $("#myform").valid(); //Validate again
         if(status==true) { 
             //Carry on
         }
         else { }
});

This works try it.

For additional resources try

http://rmurphey.com/blog/2009/01/12/jquery-validation-and-tinymce/

http://tinymce.moxiecode.com/punbb/viewtopic.php?id=21588

http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Jquery/Q_23941005.html

like image 31
Starx Avatar answered Oct 14 '22 09:10

Starx