[gnome-maps/wip/mlundblad/wikipedia-validate: 5/6] osmEditDialog: Generalize validation of empty values




commit 02f9e9d379a65bf4af214c5795fb89d457926e03
Author: Marcus Lundblad <ml update uu se>
Date:   Sun Apr 25 23:38:40 2021 +0200

    osmEditDialog: Generalize validation of empty values
    
    Make the validation handling special-casing empty text
    values generic so we won't have to implement specific
    functions shortcircuiting empty values for all fields
    that would have validation.

 src/osmEditDialog.js | 17 ++++++++---------
 1 file changed, 8 insertions(+), 9 deletions(-)
---
diff --git a/src/osmEditDialog.js b/src/osmEditDialog.js
index 5a7b108c..a28e8778 100644
--- a/src/osmEditDialog.js
+++ b/src/osmEditDialog.js
@@ -84,12 +84,6 @@ var _osmPhoneRewriteFunc = function(text) {
     }
 };
 
-/* Make sure a website field is either empty or contains a valid HTTP or HTTPS
- * URL. */
-var _osmWebsiteValidateFunc = function(text) {
-    return text === "" || Utils.isValidWebsite(text);
-}
-
 /*
  * specification of OSM edit fields
  * name: the label for the edit field (translatable)
@@ -126,7 +120,7 @@ const OSM_FIELDS = [
         name: _("Website"),
         tag: 'website',
         type: EditFieldType.TEXT,
-        validate: this._osmWebsiteValidateFunc,
+        validate: Utils.isValidWebsite,
         validateError: _("This is not a valid URL. Make sure to include http:// or https://.";),
         hint: _("The official website. Try to use the most basic form " +
                 "of a URL i.e. http://example.com instead of " +
@@ -630,7 +624,11 @@ var OSMEditDialog = GObject.registerClass({
 
     _validateTextEntry(fieldSpec, entry) {
         if (fieldSpec.validate) {
-            if (!fieldSpec.validate(entry.text)) {
+            /* also allow empty text without showing the validation warning,
+             * since we want to allow clearing out the text to unset a value
+             * (equivalent to using the delete button).
+             */
+            if (entry.text !== '' && !fieldSpec.validate(entry.text)) {
                 entry.get_style_context().add_class("warning");
             } else {
                 entry.get_style_context().remove_class("warning");
@@ -662,7 +660,8 @@ var OSMEditDialog = GObject.registerClass({
         if (fieldSpec.hint) {
             entry.secondary_icon_name = 'dialog-information-symbolic';
             entry.connect('icon-press', (entry, iconPos, event) => {
-                if (fieldSpec.validate && !fieldSpec.validate(entry.text)) {
+                if (fieldSpec.validate && entry.text !== '' &&
+                    !fieldSpec.validate(entry.text)) {
                     this._showHintPopover(entry, fieldSpec.validateError);
                 } else {
                     this._showHintPopover(entry, fieldSpec.hint);


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]