Improved support for inline validation
authorAlexander Ebert <ebert@woltlab.com>
Sun, 5 Jun 2016 12:14:51 +0000 (14:14 +0200)
committerAlexander Ebert <ebert@woltlab.com>
Sun, 5 Jun 2016 12:14:51 +0000 (14:14 +0200)
wcfsetup/install/files/js/WCF.Poll.js
wcfsetup/install/files/js/WoltLab/WCF/Ui/Message/InlineEditor.js
wcfsetup/install/files/js/WoltLab/WCF/Ui/Message/Reply.js

index f812ea81a2da4af85efac586286c95e165a1731e..9dc3d2ba98982abeb3032aca566548126b9ecbf5 100644 (file)
@@ -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;
+               }
        }
 });
 
index fcfa8edaabdb5efec10f53ce5e7f621872c463c4..62e471661f0c6d295ee95ba2f3968f3281a6a2d7 100644 (file)
@@ -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.
                 * 
index 593ae051befedf36ed333d14d64053c0a13076a9..6a07622d241bd457191a2aac9deb3dfceee9917b 100644 (file)
@@ -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);