Skip to content

Commit

Permalink
Force validation API jdorn#206
Browse files Browse the repository at this point in the history
  • Loading branch information
Bernard Labno committed Jul 30, 2014
1 parent c4cc3d2 commit 3360043
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,13 @@ else {
}
```

In some cases it's possible that value is invalid, but user has not yet interacted with the form (i.e. all fields are blank, but required).
You might want to force validation and show errors i.e. on form submit.

```
editor.validate(true);
```

By default, this will do the validation with the editor's current value.
If you want to use a different value, you can pass it in as a parameter.

Expand Down
16 changes: 14 additions & 2 deletions src/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,22 @@ JSONEditor.prototype = {
},
validate: function(value) {
if(!this.ready) throw "JSON Editor not ready yet. Listen for 'ready' event before validating";

// Custom value
if(arguments.length === 1) {
return this.validator.validate(value);
if (true === value) {
this.validation_results = this.validator.validate(this.root.getValue());
for (var name in this.editors) {
if (!this.editors.hasOwnProperty(name) || !this.editors[name]) {
continue;
}
this.editors[name].is_dirty = true;
}
this.root.showValidationErrors(this.validation_results);
return this.validation_results;
} else {
return this.validator.validate(value);
}
}
// Current value (use cached result)
else {
Expand Down

0 comments on commit 3360043

Please sign in to comment.