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




commit 5dc3d29fb5dce6e87e94c11f5f15ab27e373457b
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 | 14 ++++++--------
 1 file changed, 6 insertions(+), 8 deletions(-)
---
diff --git a/src/osmEditDialog.js b/src/osmEditDialog.js
index 5a7b108c..b5f5ad5f 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");


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