]> git.openstreetmap.org Git - nominatim-ui.git/blobdiff - src/components/UrlSubmitForm.svelte
Merge remote-tracking branch 'upstream/master'
[nominatim-ui.git] / src / components / UrlSubmitForm.svelte
index 72864b19676ee49715e155ffde8a31ee92f6ce39..a3dfaa07d5bb4f8f82b27d36ccf5bad2cd7d1e5c 100644 (file)
 
     return params;
   }
+
+  // https://developer.mozilla.org/en-US/docs/Web/Guide/HTML/HTML5/Constraint_validation
+  // doesn't support hidden fields, so we check those in an extra step
+  function validate_field(field) {
+    if (field.type === 'hidden') {
+      if (field.value.length) {
+        if (field.pattern && !field.value.match(field.pattern)) return false;
+      }
+    }
+    return field.checkValidity(); // for hidden field always true
+  }
+
+  function handle_submit(event) {
+    let form = event.target;
+
+    let allow_submit = true;
+
+    Array.prototype.slice.call(form.elements).forEach(function (field) {
+      if (!validate_field(field)) {
+        alert('Invalid input in ' + field.name);
+        allow_submit = false;
+      }
+    });
+
+    if (allow_submit) refresh_page(page, serialize_form(form));
+  }
 </script>
 
-<form on:submit|preventDefault={(e) => refresh_page(page, serialize_form(e.target))} class="form-inline" role="search" accept-charset="UTF-8" action="">
+<form on:submit|preventDefault={handle_submit} class="form-inline" role="search" accept-charset="UTF-8" action="">
+  <div class="row g-2">
     <slot></slot>
+  </div>
 </form>