|
| 1 | +<!doctype html> |
| 2 | +<html lang="en"> |
| 3 | +<head> |
| 4 | + <meta charset="utf-8"> |
| 5 | + <title>jQuery Validation plugin: integration with TinyMCE</title> |
| 6 | + <script src="../../lib/jquery.js"></script> |
| 7 | + <script src="../../dist/jquery.validate.js"></script> |
| 8 | + <script src="//tinymce.cachefly.net/4.1/tinymce.min.js"></script> |
| 9 | + <script> |
| 10 | + tinymce.init({ |
| 11 | + mode: "textareas", |
| 12 | + |
| 13 | + setup: function(editor) { |
| 14 | + editor.on('change', function(e) { |
| 15 | + tinymce.triggerSave(); |
| 16 | + $("#" + editor.id).valid(); |
| 17 | + }); |
| 18 | + } |
| 19 | + |
| 20 | + }); |
| 21 | + $(function() { |
| 22 | + var validator = $("#myform").submit(function() { |
| 23 | + // update underlying textarea before submit validation |
| 24 | + tinyMCE.triggerSave(); |
| 25 | + }).validate({ |
| 26 | + ignore: "", |
| 27 | + rules: { |
| 28 | + title: "required", |
| 29 | + content: "required" |
| 30 | + }, |
| 31 | + errorPlacement: function(label, element) { |
| 32 | + // position error label after generated textarea |
| 33 | + if (element.is("textarea")) { |
| 34 | + label.insertAfter(element.next()); |
| 35 | + } else { |
| 36 | + label.insertAfter(element) |
| 37 | + } |
| 38 | + } |
| 39 | + }); |
| 40 | + validator.focusInvalid = function() { |
| 41 | + // put focus on tinymce on submit validation |
| 42 | + if (this.settings.focusInvalid) { |
| 43 | + try { |
| 44 | + var toFocus = $(this.findLastActive() || this.errorList.length && this.errorList[0].element || []); |
| 45 | + if (toFocus.is("textarea")) { |
| 46 | + tinyMCE.get(toFocus.attr("id")).focus(); |
| 47 | + } else { |
| 48 | + toFocus.filter(":visible").focus(); |
| 49 | + } |
| 50 | + } catch (e) { |
| 51 | + // ignore IE throwing errors when focusing hidden elements |
| 52 | + } |
| 53 | + } |
| 54 | + } |
| 55 | + }) |
| 56 | + </script> |
| 57 | + <!-- /TinyMCE --> |
| 58 | +</head> |
| 59 | +<body> |
| 60 | +<form id="myform" action=""> |
| 61 | + <h3>TinyMCE4 and Validation Plugin integration example</h3> |
| 62 | + <label>Some other field</label> |
| 63 | + <input name="title"> |
| 64 | + <br> |
| 65 | + <label>Some richt text</label> |
| 66 | + <textarea id="content" name="content" rows="15" cols="80" style="width: 80%"></textarea> |
| 67 | + <br> |
| 68 | + <input type="submit" name="save" value="Submit"> |
| 69 | +</form> |
| 70 | +</body> |
| 71 | +</html> |
0 commit comments