/
AsyncValidation.svelte
164 lines (134 loc) · 3.68 KB
/
AsyncValidation.svelte
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
<script>
import {controller, NativeValidationErrors, ValidationStates} from 'pluma-svelte-forms';
import {writable} from 'svelte/store';
import Cancelable from 'p-cancelable';
const {VALID, INVALID, PENDING} = ValidationStates;
let submittedValues, passwordError, passwordIsValid;
const displayedErrors = writable(null);
const controllerState = writable(null);
let usernameError, usernameValidationState, isValidating;
let cancelable, cancelable_2;
function checkUsernameIsAvailable () {
let timeout;
return new Cancelable((resolve, reject, onCancel) => {
timeout = setTimeout(() => {
// Determine if a username is available randomly
const usernameIsAvailable = Math.round(Math.random()) === 0;
resolve(usernameIsAvailable);
}, 1500);
onCancel(() => {
clearTimeout(timeout);
});
});
}
async function checkUsername (fieldState, eventType, update) {
if (eventType === 'blur' || eventType === 'focus' || eventType === 'change') return;
if (eventType === 'submit' && fieldState.validationState === VALID) return;
if (cancelable) cancelable.cancel();
isValidating = false;
const value = fieldState.value;
if (!value) {
update({
validationState: INVALID,
error: 'Please write a username',
displayError: true
});
return;
}
if (value.length < 4) {
update({
validationState: INVALID,
error: 'Please write a longer username',
displayError: true
});
return;
}
update({validationState: PENDING});
isValidating = true;
cancelable = checkUsernameIsAvailable();
try {
const usernameIsAvailable = await cancelable;
isValidating = false;
if (usernameIsAvailable) {
update({validationState: VALID});
} else {
update({
validationState: INVALID,
error: 'Username is not available',
displayError: true
});
}
} catch (error) {
if (error.isCanceled) return;
throw error;
}
}
const formControllerSettings = {
async onSubmit (values) {
submittedValues = values;
},
validClass: 'is-valid',
invalidClass: 'is-invalid',
displayedErrors,
controllerState,
hideErrorsOnInput: true,
fields: {
username: {
externalValidator: checkUsername
}
}
}
$: usernameError = $displayedErrors?.username || null;
$: usernameValidationState = $controllerState?.fields.username.validationState;
</script>
<div class="wrap">
<h1 class="mb-4">Async validation</h1>
<form use:controller={formControllerSettings} class="mb-5" autocomplete="off">
<div class="mb-3">
<label for="input-username" class="form-label">Username</label>
<input type="text" name="username" class="form-control" id="input-username">
{#if usernameValidationState === VALID}
<div class="valid-feedback">Username is available!</div>
{:else if usernameValidationState === INVALID}
<div class="invalid-feedback">{usernameError}</div>
{:else if isValidating}
<div class="form-text" id="checking-spinner">
<div class="spinner-border spinner-border-sm" role="status"></div>
Checking if username is available...
</div>
{/if}
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
{#if submittedValues}
<h3>Submitted values</h3>
<pre>
{JSON.stringify(submittedValues, null, 2)}
</pre>
{/if}
{#if $displayedErrors}
<h3>Displayed errors</h3>
<pre>
{JSON.stringify($displayedErrors, null, 2)}
</pre>
{/if}
{#if $controllerState}
<h3>Controller state</h3>
<pre>
{JSON.stringify($controllerState, null, 2)}
</pre>
{/if}
</div>
<style>
.wrap {
max-width: 30rem;
margin: 0 auto;
}
#checking-spinner {
display: flex;
align-items: center;
}
#checking-spinner .spinner-border {
margin-right: .5rem;
}
</style>