Skip to content

Commit 2861493

Browse files
committed
Use custom validity for server-only validation
I'll admit it's a bit weird because it sets and reports validity _after_ submitting, but this way is the most accessible by default.
1 parent f033efe commit 2861493

2 files changed

Lines changed: 19 additions & 11 deletions

File tree

frontend/pages/sign-in.vue

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ watch([email, password], () => {
1313
areCredentialsWrong.value = false;
1414
});
1515
16-
async function submitSignIn() {
16+
async function submitSignIn(event: Event) {
1717
loading.value = true;
1818
1919
try {
@@ -27,6 +27,12 @@ async function submitSignIn() {
2727
catchApiErrors: {
2828
USER_CREDENTIALS_WRONG: () => {
2929
areCredentialsWrong.value = true;
30+
31+
// Wait for the custom validity to update before reporting it.
32+
// (`nextTick` doesn't wait long enough.)
33+
setTimeout(() => {
34+
(event.target as HTMLFormElement).reportValidity();
35+
});
3036
},
3137
},
3238
});
@@ -61,6 +67,9 @@ async function submitSignIn() {
6167
maxlength="254"
6268
required
6369
autofocus
70+
:custom-validity="
71+
areCredentialsWrong ? 'Incorrect email or password.' : ''
72+
"
6473
/>
6574

6675
<TextInput
@@ -69,6 +78,9 @@ async function submitSignIn() {
6978
type="password"
7079
maxlength="256"
7180
required
81+
:custom-validity="
82+
areCredentialsWrong ? 'Incorrect email or password.' : ''
83+
"
7284
>
7385
<template #after>
7486
<div class="reset-password-link-wrapper">
@@ -77,10 +89,6 @@ async function submitSignIn() {
7789
</template>
7890
</TextInput>
7991

80-
<p v-if="areCredentialsWrong" class="warning">
81-
Incorrect email or password.
82-
</p>
83-
8492
<Button type="submit">Sign In</Button>
8593
</fieldset>
8694
</form>

frontend/pages/sign-up.vue

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,11 @@ async function submitCode(event: Event) {
129129
RESOURCE_NOT_FOUND: () => {
130130
isCodeWrong.value = true;
131131
132-
const form = event.target as HTMLFormElement;
133-
form.getElementsByTagName("input")[0]?.select();
132+
// Wait for the custom validity to update before reporting it.
133+
// (`nextTick` doesn't wait long enough.)
134+
setTimeout(() => {
135+
(event.target as HTMLFormElement).reportValidity();
136+
});
134137
},
135138
},
136139
});
@@ -240,12 +243,9 @@ async function completeSignUp() {
240243
aria-label="Verification Code"
241244
required
242245
autofocus
246+
:custom-validity="isCodeWrong ? 'Incorrect verification code.' : ''"
243247
/>
244248

245-
<p v-if="isCodeWrong" class="warning">
246-
That verification code is incorrect.
247-
</p>
248-
249249
<Button type="submit">Verify</Button>
250250
</fieldset>
251251
</form>

0 commit comments

Comments
 (0)