2
0
mirror of https://github.com/frappe/books.git synced 2024-12-25 12:10:06 +00:00

fix: validateMandatory in insert

This commit is contained in:
Faris Ansari 2019-12-05 00:13:08 +05:30
parent cf1e38f59b
commit 7af5c30e7b
2 changed files with 29 additions and 0 deletions

View File

@ -44,6 +44,13 @@ class LinkValidationError extends ValidationError {
} }
} }
class MandatoryError extends ValidationError {
constructor(message) {
super(message);
this.name = 'MandatoryError';
}
}
class DatabaseError extends BaseError { class DatabaseError extends BaseError {
constructor(message) { constructor(message) {
super(500, message); super(500, message);
@ -79,5 +86,6 @@ module.exports = {
DuplicateEntryError, DuplicateEntryError,
LinkValidationError, LinkValidationError,
DatabaseError, DatabaseError,
MandatoryError,
throw: throwError throw: throwError
}; };

View File

@ -166,6 +166,26 @@ module.exports = class BaseDocument extends Observable {
} }
} }
validateInsert() {
this.validateMandatory();
}
validateMandatory() {
let mandatoryFields = this.meta.fields.filter(df => df.required);
let missingMandatoryFields = mandatoryFields.filter(df => {
let value = this[df.fieldname];
if (df.fieldtype === 'Table') {
return !value || value.length === 0;
}
return value == null || value === '';
});
if (missingMandatoryFields.length > 0) {
let fields = missingMandatoryFields.map(df => `"${df.label}"`).join(', ');
let message = frappe._('Value missing for {0}', fields);
throw new frappe.errors.MandatoryError(message);
}
}
async validateField(key, value) { async validateField(key, value) {
let field = this.meta.getField(key); let field = this.meta.getField(key);
if (field && field.fieldtype == 'Select') { if (field && field.fieldtype == 'Select') {
@ -439,6 +459,7 @@ module.exports = class BaseDocument extends Observable {
async insert() { async insert() {
await this.setName(); await this.setName();
await this.commit(); await this.commit();
await this.validateInsert();
await this.trigger('beforeInsert'); await this.trigger('beforeInsert');
let oldName = this.name; let oldName = this.name;