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:
parent
cf1e38f59b
commit
7af5c30e7b
@ -44,6 +44,13 @@ class LinkValidationError extends ValidationError {
|
||||
}
|
||||
}
|
||||
|
||||
class MandatoryError extends ValidationError {
|
||||
constructor(message) {
|
||||
super(message);
|
||||
this.name = 'MandatoryError';
|
||||
}
|
||||
}
|
||||
|
||||
class DatabaseError extends BaseError {
|
||||
constructor(message) {
|
||||
super(500, message);
|
||||
@ -79,5 +86,6 @@ module.exports = {
|
||||
DuplicateEntryError,
|
||||
LinkValidationError,
|
||||
DatabaseError,
|
||||
MandatoryError,
|
||||
throw: throwError
|
||||
};
|
||||
|
@ -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) {
|
||||
let field = this.meta.getField(key);
|
||||
if (field && field.fieldtype == 'Select') {
|
||||
@ -439,6 +459,7 @@ module.exports = class BaseDocument extends Observable {
|
||||
async insert() {
|
||||
await this.setName();
|
||||
await this.commit();
|
||||
await this.validateInsert();
|
||||
await this.trigger('beforeInsert');
|
||||
|
||||
let oldName = this.name;
|
||||
|
Loading…
Reference in New Issue
Block a user