Skip to content
This repository was archived by the owner on Nov 22, 2022. It is now read-only.

Commit 3746dd4

Browse files
Add data-labjs-validated attribute to form after failed validation
1 parent 14c4870 commit 3746dd4

File tree

2 files changed

+25
-4
lines changed

2 files changed

+25
-4
lines changed

library/src/html.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,11 @@ export class Form extends Screen {
7676

7777
// Bye!
7878
this.end('form submission')
79+
} else {
80+
// Mark form(s) as validated, but leave
81+
// the display unchanged otherwise
82+
this.options.el.querySelectorAll('form')
83+
.forEach(f => f.setAttribute('data-labjs-validated', ''))
7984
}
8085

8186
// Prevent default form behavior

library/test/html.js

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,12 @@ describe('HTML-based components', () => {
256256
return p
257257
})
258258

259+
// Validation --------------------------------------------------------------
260+
const minimalInvalidForm = '' +
261+
'<form>' +
262+
' <input type="text" name="text_input" required>' +
263+
'</form>'
264+
259265
it('validates form input using a validation function', () => {
260266
f.options.el.innerHTML = '' +
261267
'<form>' +
@@ -270,10 +276,7 @@ describe('HTML-based components', () => {
270276
})
271277

272278
it('is also sensitive to native form validation', () => {
273-
f.options.el.innerHTML = '' +
274-
'<form>' +
275-
' <input type="text" name="text_input" required>' +
276-
'</form>'
279+
f.options.el.innerHTML = minimalInvalidForm
277280

278281
assert.notOk(f.validate())
279282

@@ -284,6 +287,19 @@ describe('HTML-based components', () => {
284287

285288
assert.ok(f.validate())
286289
})
290+
291+
it('adds data-labjs-validated attribute after failed validation', () => {
292+
f.options.content = minimalInvalidForm
293+
294+
return f.run().then(() => {
295+
f.submit()
296+
assert.equal(
297+
f.options.el.querySelector('form')
298+
.getAttribute('data-labjs-validated'),
299+
'',
300+
)
301+
})
302+
})
287303
})
288304

289305
describe('Frame', () => {

0 commit comments

Comments
 (0)