[gnome-maps] osmEditDialog: Warn if website field is invalid



commit aeae5b3d0e99681a6a9632846a48294bca31d2c3
Author: James Westman <james flyingpimonster net>
Date:   Wed Nov 18 12:20:19 2020 -0600

    osmEditDialog: Warn if website field is invalid

 src/osmEditDialog.js | 29 ++++++++++++++++++++++++++++-
 1 file changed, 28 insertions(+), 1 deletion(-)
---
diff --git a/src/osmEditDialog.js b/src/osmEditDialog.js
index 4504c7b5..d5647267 100644
--- a/src/osmEditDialog.js
+++ b/src/osmEditDialog.js
@@ -84,6 +84,12 @@ 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)
@@ -120,6 +126,8 @@ const OSM_FIELDS = [
         name: _("Website"),
         tag: 'website',
         type: EditFieldType.TEXT,
+        validate: this._osmWebsiteValidateFunc,
+        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 " +
                 "http://example.com/index.html.";)
@@ -620,6 +628,16 @@ var OSMEditDialog = GObject.registerClass({
         }
     }
 
+    _validateTextEntry(fieldSpec, entry) {
+        if (fieldSpec.validate) {
+            if (!fieldSpec.validate(entry.text)) {
+                entry.get_style_context().add_class("warning");
+            } else {
+                entry.get_style_context().remove_class("warning");
+            }
+        }
+    }
+
     _addOSMEditTextEntry(fieldSpec, value) {
         this._addOSMEditLabel(fieldSpec);
 
@@ -633,13 +651,22 @@ var OSMEditDialog = GObject.registerClass({
             if (fieldSpec.rewriteFunc)
                 entry.text = fieldSpec.rewriteFunc(entry.text);
             this._osmObject.set_tag(fieldSpec.tag, entry.text);
+
+            this._validateTextEntry(fieldSpec, entry);
+
             this._nextButton.sensitive = true;
         });
 
+        this._validateTextEntry(fieldSpec, entry);
+
         if (fieldSpec.hint) {
             entry.secondary_icon_name = 'dialog-information-symbolic';
             entry.connect('icon-press', (entry, iconPos, event) => {
-                this._showHintPopover(entry, fieldSpec.hint);
+                if (fieldSpec.validate && !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]