Skip to content
Snippets Groups Projects
Commit 63305084 authored by Hermann's avatar Hermann
Browse files

validate creation forms at beginning to show required fields

parent f55d4a01
No related branches found
No related tags found
No related merge requests found
......@@ -62,6 +62,7 @@ export default class EditView extends ItemView {
});
// delete objectSchema.properties['_id'];
console.log(this.ajv.addSchema(objectSchema, 'schema'));
this.validate();
}).catch((error) => { console.log(error); });
}
......@@ -78,27 +79,7 @@ export default class EditView extends ItemView {
console.log(this.data);
// validate against schema
const validate = this.ajv.getSchema('schema');
// sometimes the schema loading does not work or is not finished
// before the first edit, this is to prevent crashes
if (validate) {
this.valid = validate(this.data);
console.log(validate.errors);
if (this.valid) {
Object.keys(this.errors).forEach((field) => {
this.errors[field] = [];
});
} else {
// get errors for respective fields
Object.keys(this.errors).forEach((field) => {
const errors = validate.errors.filter(error =>
`.${field}` === error.dataPath);
this.errors[field] = errors.map(error => error.message);
});
}
}
this.validate();
},
getErrors: () => this.errors[attrs.name],
value: this.data[attrs.name],
......@@ -109,6 +90,30 @@ export default class EditView extends ItemView {
return boundFormelement;
}
validate() {
// validate against schema
const validate = this.ajv.getSchema('schema');
// sometimes the schema loading does not work or is not finished
// before the first edit, this is to prevent crashes
if (validate) {
this.valid = validate(this.data);
console.log(validate.errors);
if (this.valid) {
Object.keys(this.errors).forEach((field) => {
this.errors[field] = [];
});
} else {
// get errors for respective fields
Object.keys(this.errors).forEach((field) => {
const errors = validate.errors.filter(error =>
`.${field}` === error.dataPath);
this.errors[field] = errors.map(error => error.message);
});
}
}
m.redraw();
}
renderPage(page) {
return Object.keys(page).map((key) => {
const field = page[key];
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment