Skip to content

Commit 57cdaf5

Browse files
Merge pull request #1593 from germanbisurgi/feature/editor-show-error-messages
Feature/editor show error messages
2 parents deca811 + 6ece86c commit 57cdaf5

File tree

4 files changed

+81
-0
lines changed

4 files changed

+81
-0
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,12 @@ const errors = editor.validate({
409409
});
410410
```
411411

412+
Display validation errors on demand
413+
414+
```javascript
415+
editor.showValidationErrors();
416+
```
417+
412418
### Listen for Changes
413419

414420
The `change` event is fired whenever the editor's value changes.

src/core.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,15 @@ export class JSONEditor {
411411
styleSheet.replaceSync(cssText)
412412
shadowRoot.adoptedStyleSheets = [...shadowRoot.adoptedStyleSheets, styleSheet]
413413
}
414+
415+
showValidationErrors () {
416+
const errors = this.validate()
417+
418+
Object.values(this.editors).forEach(editor => {
419+
editor.is_dirty = true
420+
editor.showValidationErrors(errors)
421+
})
422+
}
414423
}
415424

416425
JSONEditor.defaults = defaults

tests/codeceptjs/core_test.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,18 @@ const { DEFAULT_WAIT_TIME } = require('./test-config')
55

66
Feature('core')
77

8+
Scenario('should show all validation errors on demand @editor-show-validation-errors', async ({ I }) => {
9+
I.amOnPage('editor-show-validation-errors.html')
10+
I.waitForElement('.je-ready')
11+
I.dontSee('Value must be the constant value')
12+
I.dontSee('Value must be at least 4 characters long')
13+
I.dontSee('Value must be at least 4')
14+
I.click('#show-validation-errors')
15+
I.waitForText('Value must be the constant value')
16+
I.waitForText('Value must be at least 4 characters long')
17+
I.waitForText('Value must be at least 4')
18+
})
19+
820
Scenario('should enforce const @enforce_const', async ({ I }) => {
921
I.amOnPage('enforce-const.html')
1022
I.waitForElement('.je-ready')
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="utf-8"/>
5+
<title>showValidationErrors</title>
6+
<script src="../../dist/jsoneditor.js"></script>
7+
<link rel="stylesheet" id="theme-link" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css">
8+
<link rel="stylesheet" id="iconlib-link" href="https://use.fontawesome.com/releases/v5.6.1/css/all.css">
9+
</head>
10+
<body>
11+
12+
<div class="container">
13+
<div id='editor-container'></div>
14+
<div class="btn btn-success" id="show-validation-errors">Show validation errors</div>
15+
</div>
16+
17+
<script>
18+
const editorContainer = document.querySelector('#editor-container')
19+
const schema = {
20+
"type": "object",
21+
"properties": {
22+
"boolean-checkbox": {
23+
"title": "boolean-checkbox",
24+
"type": "boolean",
25+
"format": "checkbox",
26+
"const": true,
27+
"default": false
28+
},
29+
"minLength": {
30+
"type": "string",
31+
"minLength": 4,
32+
},
33+
"minimum": {
34+
"type": "number",
35+
"minimum": 4,
36+
}
37+
}
38+
}
39+
40+
const editor = new JSONEditor(editorContainer, {
41+
schema: schema,
42+
theme: 'bootstrap4',
43+
iconlib: 'fontawesome'
44+
})
45+
46+
const showValidationErrors = document.querySelector('#show-validation-errors')
47+
48+
showValidationErrors.addEventListener('click', () => {
49+
editor.showValidationErrors()
50+
})
51+
</script>
52+
53+
</body>
54+
</html>

0 commit comments

Comments
 (0)