Permalink
Browse files

Merge branch 'wip-mdl-28987-m20' of git://github.com/rajeshtaneja/moo…

…dle into MOODLE_20_STABLE
  • Loading branch information...
2 parents 85ae284 + 39f5dde commit bede2a2a3491b325baa2fcda413180f9b81fbb17 @stronk7 stronk7 committed Sep 6, 2011
Showing with 65 additions and 9 deletions.
  1. +4 −1 lib/editor/tinymce/lib.php
  2. +16 −0 lib/editor/tinymce/module.js
  3. +29 −1 lib/form/editor.php
  4. +16 −7 lib/formslib.php
@@ -167,7 +167,10 @@ protected function get_init_params($elementid, array $options=null) {
$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;
}
}
@@ -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();
+ });
+ };
+};
View
@@ -86,6 +86,24 @@ function setSubdirs($allow) {
$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 @@ function toHtml() {
$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 .= '<div><textarea id="'.$id.'" name="'.$elname.'[text]" rows="'.$rows.'" cols="'.$cols.'">';
+ //Apply editor validation if required field
+ $editorrules = '';
+ if (!is_null($this->getAttribute('onblur')) && !is_null($this->getAttribute('onchange'))) {
+ $editorrules = 'onblur="'.htmlspecialchars($this->getAttribute('onblur')).'" onchange="'.htmlspecialchars($this->getAttribute('onchange')).'"';
+ }
+ $str .= '<div><textarea id="'.$id.'" name="'.$elname.'[text]" rows="'.$rows.'" cols="'.$cols.'"'.$editorrules.'>';
$str .= s($text);
$str .= '</textarea></div>';
View
@@ -1690,8 +1690,16 @@ function getValidationScript()
}
}
}
+ //for editor element, [text] is appended to the name.
+ if ($element->getType() == 'editor') {
+ $elementName .= '[text]';
+ //Add format to rule as moodleform check which format is supported by browser
+ //it is not set anywhere... So small hack to make sure we pass it down to quickform
+ if (is_null($rule['format'])) {
+ $rule['format'] = $element->getFormat();
+ }
+ }
// Fix for bug displaying errors for elements in a group
- //$test[$elementName][] = $registry->getValidationScript($element, $elementName, $rule);
$test[$elementName][0][] = $registry->getValidationScript($element, $elementName, $rule);
$test[$elementName][1]=$element;
//end of fix
@@ -2374,17 +2382,18 @@ function validate($value, $options = null) {
/**
* This function returns Javascript code used to build client-side validation.
* It checks if an element is not empty.
- * Note, that QuickForm does not know how to work with editor text field and builds not correct
- * JS code for validation. If client check is enabled for editor field it will not be validated
- * on client side no matter what this function returns.
*
- * @param mixed $options Not used yet
+ * @param int $format
* @return array
*/
- function getValidationScript($options = null) {
+ function getValidationScript($format = null) {
global $CFG;
if (!empty($CFG->strictformsrequired)) {
- return array('', "{jsVar}.replace(/^\s+$/g, '') == ''");
+ if (!empty($format) && $format == FORMAT_HTML) {
+ return array('', "{jsVar}.replace(/(<[^img|hr|canvas]+>)|&nbsp;|\s+/ig, '') == ''");
+ } else {
+ return array('', "{jsVar}.replace(/^\s+$/g, '') == ''");
+ }
} else {
return array('', "{jsVar} == ''");
}

0 comments on commit bede2a2

Please sign in to comment.