diff --git a/lib/editor/tinymce/lib.php b/lib/editor/tinymce/lib.php index b0f3bf00a4e..4be37d416c4 100644 --- a/lib/editor/tinymce/lib.php +++ b/lib/editor/tinymce/lib.php @@ -167,7 +167,10 @@ class tinymce_texteditor extends texteditor { $params['file_browser_callback'] = "M.editor_tinymce.filepicker"; } } - + //Add onblur event for client side text validation + if (!empty($options['required'])) { + $params['init_instance_callback'] = 'M.editor_tinymce.onblur_event'; + } return $params; } } diff --git a/lib/editor/tinymce/module.js b/lib/editor/tinymce/module.js index 7db01985456..2754961545d 100644 --- a/lib/editor/tinymce/module.js +++ b/lib/editor/tinymce/module.js @@ -72,3 +72,19 @@ M.editor_tinymce.filepicker = function(target_id, url, type, win) { }); }; +M.editor_tinymce.onblur_event = function(ed) { + //Attach event only after tinymce is intialized. + if (ed.onInit != undefined) { + var s = ed.settings; + //Save before event is attached, so that if this event is not generated then textarea should + //have loaded contents and submitting form should not throw error. + ed.save(); + + //Attach blur event for tinymce to save contents to textarea + var doc = s.content_editable ? ed.getBody() : (tinymce.isGecko ? ed.getDoc() : ed.getWin()); + tinymce.dom.Event.add(doc, 'blur', function() { + //save contents to textarea before calling validation script. + ed.save(); + }); + }; +}; diff --git a/lib/form/editor.php b/lib/form/editor.php index 6e1f12a2647..eeec855fc5c 100644 --- a/lib/form/editor.php +++ b/lib/form/editor.php @@ -86,6 +86,24 @@ class MoodleQuickForm_editor extends HTML_QuickForm_element { $this->_options['subdirs'] = $allow; } + /** + * Returns editor format + * + * @return int. + */ + function getFormat() { + return $this->_values['format']; + } + + /** + * Checks if editor used is tinymce and is required field + * + * @return true if required field. + */ + function isRequired() { + return (isset($this->_options['required']) && $this->_options['required']); + } + function setHelpButton($_helpbuttonargs, $function='_helpbutton') { if (!is_array($_helpbuttonargs)) { $_helpbuttonargs = array($_helpbuttonargs); @@ -198,13 +216,23 @@ class MoodleQuickForm_editor extends HTML_QuickForm_element { $fpoptions['link'] = $link_options; } + //If editor is required and tinymce, then set required_tinymce option to initalize tinymce validation. + if (($editor instanceof tinymce_texteditor) && !is_null($this->getAttribute('onchange'))) { + $this->_options['required'] = true; + } + /// print text area - TODO: add on-the-fly switching, size configuration, etc. $editor->use_editor($id, $this->_options, $fpoptions); $rows = empty($this->_attributes['rows']) ? 15 : $this->_attributes['rows']; $cols = empty($this->_attributes['cols']) ? 80 : $this->_attributes['cols']; - $str .= '