From f01c6a0929548b8178a500f385dff87898411b6f Mon Sep 17 00:00:00 2001 From: Alexander Ebert Date: Sun, 5 Jun 2016 14:14:51 +0200 Subject: [PATCH] Improved support for inline validation --- wcfsetup/install/files/js/WCF.Poll.js | 22 +++++++++ .../js/WoltLab/WCF/Ui/Message/InlineEditor.js | 45 +++++++++++++++++++ .../files/js/WoltLab/WCF/Ui/Message/Reply.js | 10 ++--- 3 files changed, 72 insertions(+), 5 deletions(-) diff --git a/wcfsetup/install/files/js/WCF.Poll.js b/wcfsetup/install/files/js/WCF.Poll.js index f812ea81a2..9dc3d2ba98 100644 --- a/wcfsetup/install/files/js/WCF.Poll.js +++ b/wcfsetup/install/files/js/WCF.Poll.js @@ -66,6 +66,7 @@ WCF.Poll.Management = Class.extend({ WCF.System.Event.addListener('com.woltlab.wcf.redactor2', 'reset_' + editorId, this._reset.bind(this)); WCF.System.Event.addListener('com.woltlab.wcf.redactor2', 'submit_' + editorId, this._submit.bind(this)); + WCF.System.Event.addListener('com.woltlab.wcf.redactor2', 'validate_' + editorId, this._validate.bind(this)); } else { this._container.closest('form').submit($.proxy(this._submit, this)); @@ -265,6 +266,27 @@ WCF.Poll.Management = Class.extend({ require(['WoltLab/WCF/Date/Picker'], (function(UiDatePicker) { UiDatePicker.clear('pollEndTime_' + this._editorId); }).bind(this)); + }, + + _validate: function(data) { + var question = elById('pollQuestion_' + this._editorId); + if (question.value.trim() === '') { + // no question provided, ignore + return; + } + + // get options + var hasOptions = false; + elBySelAll('li input[type="text"]', this._container[0], function(input) { + if (input.value.trim() !== '') { + hasOptions = true; + } + }); + + if (hasOptions === false) { + data.api.throwError(this._container[0], WCF.Language.get('wcf.global.form.error.empty')); + data.valid = false; + } } }); diff --git a/wcfsetup/install/files/js/WoltLab/WCF/Ui/Message/InlineEditor.js b/wcfsetup/install/files/js/WoltLab/WCF/Ui/Message/InlineEditor.js index fcfa8edaab..62e471661f 100644 --- a/wcfsetup/install/files/js/WoltLab/WCF/Ui/Message/InlineEditor.js +++ b/wcfsetup/install/files/js/WoltLab/WCF/Ui/Message/InlineEditor.js @@ -416,6 +416,12 @@ define( var id = this._getEditorId(); EventHandler.fire('com.woltlab.wcf.redactor2', 'getText_' + id, parameters.data); + + if (!this._validate(parameters)) { + // validation failed + return; + } + EventHandler.fire('com.woltlab.wcf.redactor2', 'submit_' + id, parameters); Ajax.api(this, { @@ -426,6 +432,45 @@ define( this._hideEditor(); }, + /** + * Validates the message and invokes listeners to perform additional validation. + * + * @param {Object} parameters request parameters + * @return {boolean} validation result + * @protected + */ + _validate: function(parameters) { + // remove all existing error elements + var errorMessages = elByClass('innerError', this._activeElement); + while (errorMessages.length) { + elRemove(errorMessages[0]); + } + + var data = { + api: this, + parameters: parameters, + valid: true + }; + + EventHandler.fire('com.woltlab.wcf.redactor2', 'validate_' + this._getEditorId(), data); + + return (data.valid !== false); + }, + + /** + * Throws an error by adding an inline error to target element. + * + * @param {Element} element erroneous element + * @param {string} message error message + */ + throwError: function(element, message) { + var error = elCreate('small'); + error.className = 'innerError'; + error.textContent = message; + + DomUtil.insertAfter(error, element); + }, + /** * Shows the update message. * diff --git a/wcfsetup/install/files/js/WoltLab/WCF/Ui/Message/Reply.js b/wcfsetup/install/files/js/WoltLab/WCF/Ui/Message/Reply.js index 593ae051be..6a07622d24 100644 --- a/wcfsetup/install/files/js/WoltLab/WCF/Ui/Message/Reply.js +++ b/wcfsetup/install/files/js/WoltLab/WCF/Ui/Message/Reply.js @@ -93,7 +93,7 @@ define(['Ajax', 'Core', 'EventHandler', 'Language', 'Dom/Util', 'Ui/Notification // remove all existing error elements var errorMessages = elByClass('innerError', this._container); while (errorMessages.length) { - errorMessages[0].parentNode.removeChild(errorMessages[0]); + elRemove(errorMessages[0]); } // check if editor contains actual content @@ -103,10 +103,10 @@ define(['Ajax', 'Core', 'EventHandler', 'Language', 'Dom/Util', 'Ui/Notification } var data = { - 'api': this, - 'editor': this._getEditor(), - 'message': this._getEditor().code.get(), - 'valid': true + api: this, + editor: this._getEditor(), + message: this._getEditor().code.get(), + valid: true }; EventHandler.fire('com.woltlab.wcf.redactor2', 'validate_text', data); -- 2.20.1