Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add manual validation trigger #79

Merged
merged 3 commits into from
Jan 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,14 @@ input.addEventListener('auto-check-complete', function(event) {
[CSRF]: https://en.wikipedia.org/wiki/Cross-site_request_forgery
[Response]: https://developer.mozilla.org/en-US/docs/Web/API/Response

## Manually Trigger Validation

The `triggerValidation()` function can be used to manually trigger the `<auto-check>` element.

```js
document.getElementById('input-element').closest('auto-check').triggerValidation()
```

## Browser support

Browsers without native [custom element support][support] require a [polyfill][].
Expand Down
9 changes: 9 additions & 0 deletions custom-elements.json
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,15 @@
"kind": "field",
"name": "onloadend"
},
{
"kind": "method",
"name": "triggerValidation",
"return": {
"type": {
"text": "void"
}
}
},
{
"kind": "field",
"name": "input",
Expand Down
11 changes: 10 additions & 1 deletion src/auto-check-element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ export class AutoCheckElement extends HTMLElement {

input.addEventListener('blur', changeHandler)
input.addEventListener('input', changeHandler)
input.addEventListener('triggervalidation', changeHandler)
input.autocomplete = 'off'
input.spellcheck = false
}
Expand All @@ -134,6 +135,13 @@ export class AutoCheckElement extends HTMLElement {
}
}

triggerValidation(): void {
const input = this.input
if (!input) return

input.dispatchEvent(new CustomEvent('triggervalidation'))
}

static get observedAttributes(): string[] {
return ['required']
}
Expand Down Expand Up @@ -226,7 +234,8 @@ function handleChange(checker: () => void, event: Event) {
autoCheckElement.onlyValidateOnBlur &&
!autoCheckElement.validateOnKeystroke &&
autoCheckElement.hasAttribute('dirty')) || // Only validate on blur if only-validate-on-blur is set, input is dirty, and input is not current validating on keystroke
(event.type === 'input' && autoCheckElement.onlyValidateOnBlur && autoCheckElement.validateOnKeystroke) // Only validate on key inputs in only-validate-on-blur mode if validate-on-keystroke is set (when input is invalid)
(event.type === 'input' && autoCheckElement.onlyValidateOnBlur && autoCheckElement.validateOnKeystroke) || // Only validate on key inputs in only-validate-on-blur mode if validate-on-keystroke is set (when input is invalid)
event.type === 'triggervalidation' // Trigger validation manually
) {
setLoadingState(event)
checker()
Expand Down
42 changes: 42 additions & 0 deletions test/auto-check.js
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,48 @@ describe('auto-check element', function () {
})
})

describe('manually triggering validation', function () {
let checker
let input

beforeEach(function () {
const container = document.createElement('div')
container.innerHTML = `
<auto-check csrf="foo" src="/success" required>
<input id="input-field" value="hub">
</auto-check>`
document.body.append(container)

checker = document.querySelector('auto-check')
input = checker.querySelector('input')
})

it('emits on manual trigger', async function () {
const events = []
input.addEventListener('auto-check-start', event => events.push(event.type))

document.getElementById('input-field').closest('auto-check').triggerValidation()

assert.deepEqual(events, ['auto-check-start'])
})

it('does not emit on manual trigger if input is empty', async function () {
const events = []
input.addEventListener('auto-check-start', event => events.push(event.type))

input.value = ''
document.getElementById('input-field').closest('auto-check').triggerValidation()

assert.deepEqual(events, [])
})

afterEach(function () {
document.body.innerHTML = ''
checker = null
input = null
})
})

describe('using HTTP GET', function () {
let checker
let input
Expand Down
Loading