Skip to content

Commit

Permalink
feat(ui): show errors for client-side validation
Browse files Browse the repository at this point in the history
Thank you, @a0eoc, for the help.

Fix #243
Fix #273
  • Loading branch information
vednoc committed Oct 15, 2023
1 parent 577ceae commit 0c0f4fc
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 17 deletions.
17 changes: 14 additions & 3 deletions web/typescript/page/edit.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,21 @@
export function checkMaxLength() {
type input = HTMLTextAreaElement | HTMLInputElement;
type error = HTMLParagraphElement;

const validate = (el: input, max: number) => {
el.setCustomValidity(el.value.length > max
? `Your input must be up to ${max} characters.`
: '');
const curr = el.value.length;
if (curr > max) {
el.setCustomValidity(`Your input must be up to ${max} characters.`);
message(el, `Input is too long. Characters used: ${curr}/${max}`);
} else {
el.setCustomValidity('');
message(el, '');
}
}

const message = (el: input, msg: string) => {
const e = document.querySelector(`.danger.${el.name}`) as error;
if (e) e.innerText = msg;
}

[...document.querySelectorAll('[maxlength]')].forEach((el: input) => {
Expand Down
3 changes: 3 additions & 0 deletions web/views/review/create.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@
style="min-height: 120px"
aria-describedby="comment-hint"
>{{ .Review.Comment }}</textarea>
<p class="danger comment" role="alert" style="margin-top: -1rem">
{{ with .Error }}{{ . }}{{ end }}
</p>

<div class="mt:m">
<button class="btn primary mr:s" type="submit">Confirm</button>
Expand Down
18 changes: 13 additions & 5 deletions web/views/style/add.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,10 @@
required maxlength="50"
type="text" name="name" id="name" value="{{ .Style.Name }}"
placeholder="e.g. UserStyles.world tweaks">
{{ with .err.Name }}<p class="danger" role="alert">{{ . }}</p>{{ end }}
{{ with .dupName }}<p class="danger" role="alert">{{ . }}</p>{{ end }}
<p class="danger name" role="alert">
{{ with .err.Name }}{{ . }}{{ end }}
</p>
{{ with .dupName }}<p class="danger name" role="alert">{{ . }}</p>{{ end }}

<label for="description">Description</label>
<i class="fg:3" id="description-hint">Short description of what your userstyle does in plain text. Will be used for SEO and rich embeds.</i>
Expand All @@ -40,7 +42,9 @@
aria-describedby="description-hint"
placeholder="e.g. UI experiments for UserStyles.world."
>{{ .Style.Description }}</textarea>
{{ with .err.Description }}<p class="danger" role="alert">{{ . }}</p>{{ end }}
<p class="danger description" role="alert">
{{ with .err.Description }}{{ . }}{{ end }}
</p>

<label for="notes">Notes</label>
<i class="fg:3" id="notes-hint">Features, requirements, instructions, links, changelog, etc. Supports {{ template "partials/markdown" . }} syntax.</i>
Expand All @@ -50,7 +54,9 @@
aria-describedby="notes-hint"
placeholder="e.g. Please raise issues in the GitHub repository."
>{{ .Style.Notes }}</textarea>
{{ with .err.Notes }}<p class="danger" role="alert">{{ . }}</p>{{ end }}
<p class="danger notes" role="alert">
{{ with .err.Notes }}{{ . }}{{ end }}
</p>

<label for="homepage">Homepage</label>
<i class="fg:3" id="homepage-hint">Link to userstyle's homepage, bug tracker, etc.</i>
Expand Down Expand Up @@ -93,7 +99,9 @@
aria-describedby="category-hint"
value="{{ .Style.Category }}"
placeholder="e.g. test.userstyles.world -> userstyles">
{{ with .err.Category }}<p class="danger" role="alert">{{ . }}</p>{{ end }}
<p class="danger category" role="alert">
{{ with .err.Category }}{{ . }}{{ end }}
</p>

<label for="code">Source code</label>
<textarea
Expand Down
16 changes: 12 additions & 4 deletions web/views/style/edit.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@
required maxlength="50"
type="text" name="name" id="name" value="{{ .Style.Name }}"
placeholder="e.g. UserStyles.world tweaks">
{{ with .err.Name }}<p class="danger" role="alert">{{ . }}</p>{{ end }}
<p class="danger name" role="alert">
{{ with .err.Name }}{{ . }}{{ end }}
</p>
{{ with .dupName }}<p class="danger" role="alert">{{ . }}</p>{{ end }}

<label for="description">Description</label>
Expand All @@ -35,7 +37,9 @@
aria-describedby="description-hint"
placeholder="e.g. UI experiments for UserStyles.world."
>{{ .Style.Description }}</textarea>
{{ with .err.Description }}<p class="danger" role="alert">{{ . }}</p>{{ end }}
<p class="danger description" role="alert">
{{ with .err.Description }}{{ . }}{{ end }}
</p>

<label for="notes">Notes</label>
<i class="fg:3" id="notes-hint">Features, requirements, instructions, links, changelog, etc. Supports {{ template "partials/markdown" . }} syntax.</i>
Expand All @@ -45,7 +49,9 @@
aria-describedby="notes-hint"
placeholder="e.g. Please raise issues in the GitHub repository."
>{{ .Style.Notes }}</textarea>
{{ with .err.Notes }}<p class="danger" role="alert">{{ . }}</p>{{ end }}
<p class="danger notes" role="alert">
{{ with .err.Notes }}{{ . }}{{ end }}
</p>

<label for="homepage">Homepage</label>
<i class="fg:3" id="homepage-hint">Link to userstyle's homepage, bug tracker, etc.</i>
Expand Down Expand Up @@ -113,7 +119,9 @@
aria-describedby="category-hint"
value="{{ .Style.Category }}"
placeholder="e.g. test.userstyles.world -> userstyles">
{{ with .err.Category }}<p class="danger" role="alert">{{ . }}</p>{{ end }}
<p class="danger category" role="alert">
{{ with .err.Category }}{{ . }}{{ end }}
</p>

<label for="code">Source code</label>
<textarea
Expand Down
16 changes: 12 additions & 4 deletions web/views/style/import.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@
required maxlength="50"
type="text" name="name" id="name" value="{{ .Style.Name }}"
placeholder="e.g. UserStyles.world tweaks">
{{ with .err.Name }}<p class="danger" role="alert">{{ . }}</p>{{ end }}
<p class="danger name" role="alert">
{{ with .err.Name }}{{ . }}{{ end }}
</p>
{{ with .dupName }}<p class="danger" role="alert">{{ . }}</p>{{ end }}

<label for="description">Description</label>
Expand All @@ -36,7 +38,9 @@
aria-describedby="description-hint"
placeholder="e.g. UI experiments for UserStyles.world."
>{{ .Style.Description }}</textarea>
{{ with .err.Description }}<p class="danger" role="alert">{{ . }}</p>{{ end }}
<p class="danger description" role="alert">
{{ with .err.Description }}{{ . }}{{ end }}
</p>

<label for="notes">Notes</label>
<i class="fg:3" id="notes-hint">Features, requirements, instructions, links, changelog, etc. Supports {{ template "partials/markdown" . }} syntax.</i>
Expand All @@ -46,7 +50,9 @@
aria-describedby="notes-hint"
placeholder="e.g. Please raise issues in the GitHub repository."
>{{ .Style.Notes }}</textarea>
{{ with .err.Notes }}<p class="danger" role="alert">{{ . }}</p>{{ end }}
<p class="danger notes" role="alert">
{{ with .err.Notes }}{{ . }}{{ end }}
</p>

<label for="homepage">Homepage</label>
<i class="fg:3" id="homepage-hint">Link to userstyle's homepage, bug tracker, etc.</i>
Expand Down Expand Up @@ -89,7 +95,9 @@
aria-describedby="category-hint"
value="{{ .Style.Category }}"
placeholder="e.g. test.userstyles.world -> userstyles">
{{ with .err.Category }}<p class="danger" role="alert">{{ . }}</p>{{ end }}
<p class="danger category" role="alert">
{{ with .err.Category }}{{ . }}{{ end }}
</p>

<label for="code">Source code</label>
<textarea
Expand Down
4 changes: 3 additions & 1 deletion web/views/user/account.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,9 @@
aria-describedby="biography-hint">
{{- .Params.Biography -}}
</textarea>
{{ with .errBio }}<p class="danger" role="alert">{{ . }}</p>{{ end }}
<p class="danger biography" role="alert">
{{ with .errBio }}{{ . }}{{ end }}
</p>
<div class="Form-control">
<button
type="submit"
Expand Down

0 comments on commit 0c0f4fc

Please sign in to comment.