Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ee073cf

Browse files
committed
Support asynchronous validity error messages
Allow the error message to be retrieved from the server response.
1 parent 4d473e0 commit ee073cf

File tree

3 files changed

+35
-14
lines changed

3 files changed

+35
-14
lines changed

README.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,10 @@ import '@github/auto-check-element'
2323
Provide a URL and a CSRF token and the autocheck component will show validation confirmations and validation errors.
2424

2525
The endpoint should respond to POST requests with:
26-
- a 200 HTTP status code if the provided value if valid.
27-
- a 422 HTTP status code if the provided value is invalid.
28-
- a optional error message in the body and a `Content-Type` header with a value of `text/html; fragment`.
26+
27+
- a 200 HTTP status code if the provided value if valid.
28+
- a 422 HTTP status code if the provided value is invalid.
29+
- a optional error message in the body and a `Content-Type` header with a value of `text/html; fragment`.
2930

3031
## Events
3132

@@ -62,10 +63,9 @@ input.addEventListener('auto-check-success', async function(event) {
6263
console.log('Validation passed', message)
6364
})
6465
input.addEventListener('auto-check-error', function(event) {
65-
// Extract text response and invalidate the input with the custom message.
66-
const message = await event.detail.response.text()
67-
event.detail.setValidity(message)
68-
console.log('Validation failed', message)
66+
// Asynchronously extract text response and invalidate the input.
67+
const {response, setValidity} = event.detail
68+
setValidity(response.text())
6969
})
7070
input.addEventListener('auto-check-complete', function(event) {
7171
console.log('Validation complete', event)

src/index.js

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ async function check(autoCheckElement: AutoCheckElement) {
160160
method: 'POST',
161161
body
162162
})
163-
processResponse(response, input, autoCheckElement.required)
163+
await processResponse(response, input, autoCheckElement.required)
164164
state.controller = null
165165
input.dispatchEvent(new CustomEvent('auto-check-complete', {bubbles: true}))
166166
} catch (error) {
@@ -171,7 +171,7 @@ async function check(autoCheckElement: AutoCheckElement) {
171171
}
172172
}
173173

174-
function processResponse(response: Response, input: HTMLInputElement, required: boolean) {
174+
async function processResponse(response: Response, input: HTMLInputElement, required: boolean) {
175175
if (response.status === 200) {
176176
if (required) {
177177
input.setCustomValidity('')
@@ -187,8 +187,8 @@ function processResponse(response: Response, input: HTMLInputElement, required:
187187
return
188188
}
189189

190-
let message = 'Input is not valid'
191-
const setValidity = text => (message = text)
190+
const messages = []
191+
const setValidity = result => messages.push(result)
192192
input.dispatchEvent(
193193
new CustomEvent('auto-check-error', {
194194
bubbles: true,
@@ -198,9 +198,10 @@ function processResponse(response: Response, input: HTMLInputElement, required:
198198
}
199199
})
200200
)
201-
if (required) {
202-
input.setCustomValidity(message)
203-
}
201+
202+
if (!required) return
203+
const message = messages.length ? await Promise.race(messages) : 'Input is not valid'
204+
input.setCustomValidity(message)
204205
}
205206

206207
if (!window.customElements.get('auto-check')) {

test/test.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,30 @@ describe('auto-check element', function() {
8787
})
8888
})
8989
await error
90+
await once(input, 'auto-check-complete')
9091
assert(!input.validity.valid)
9192
assert.equal('A custom error', input.validationMessage)
9293
})
9394

95+
it('uses the server response as the error message', async function() {
96+
const autoCheck = document.querySelector('auto-check')
97+
autoCheck.src = '/fail'
98+
autoCheck.required = true
99+
const input = document.querySelector('input')
100+
const error = new Promise(resolve => {
101+
triggerChange(input, 'hub')
102+
input.addEventListener('auto-check-error', event => {
103+
const {response, setValidity} = event.detail
104+
setValidity(response.json().then(obj => obj.text))
105+
resolve()
106+
})
107+
})
108+
await error
109+
await once(input, 'auto-check-complete')
110+
assert(!input.validity.valid)
111+
assert.equal('This is a error', input.validationMessage)
112+
})
113+
94114
it('customizes the in-flight message', async function() {
95115
const autoCheck = document.querySelector('auto-check')
96116
autoCheck.src = '/fail'

0 commit comments

Comments
 (0)