2 import { refresh_page } from '../lib/stores.js';
6 function serialize_form(form) {
7 var params = new URLSearchParams();
9 Array.prototype.slice.call(form.elements).forEach(function (field) {
10 if (!field.name || field.disabled || ['submit', 'button'].indexOf(field.type) > -1) return;
12 if (['checkbox', 'radio'].indexOf(field.type) > -1 && !field.checked) return;
13 if (typeof field.value === 'undefined' || field.value === '') return;
15 params.set(field.name, field.value);
21 // https://developer.mozilla.org/en-US/docs/Web/Guide/HTML/HTML5/Constraint_validation
22 // doesn't support hidden fields, so we check those in an extra step
23 function validate_field(field) {
24 if (field.type === 'hidden') {
25 if (field.value.length) {
26 if (field.pattern && !field.value.match(field.pattern)) return false;
29 return field.checkValidity(); // for hidden field always true
32 function handle_submit(event) {
33 let form = event.target;
35 let allow_submit = true;
37 Array.prototype.slice.call(form.elements).forEach(function (field) {
38 if (!validate_field(field)) {
39 alert('Invalid input in ' + field.name);
44 if (allow_submit) refresh_page(page, serialize_form(form));
48 <form on:submit|preventDefault={handle_submit} class="form-inline" role="search" accept-charset="UTF-8" action="">