refactor: Commonify autoname validation
- Validate autoname for Customize Form
This commit is contained in:
parent
746e017aef
commit
8cd372d90b
5 changed files with 110 additions and 85 deletions
|
|
@ -57,8 +57,8 @@ frappe.ui.form.on('DocType', {
|
|||
frm.get_docfield('fields', 'in_list_view').label = frm.doc.istable ?
|
||||
__('In Grid View') : __('In List View');
|
||||
|
||||
frm.events.autoname(frm);
|
||||
frm.events.set_naming_rule_description(frm);
|
||||
frm.cscript.autoname(frm);
|
||||
frm.cscript.set_naming_rule_description(frm);
|
||||
},
|
||||
|
||||
istable: (frm) => {
|
||||
|
|
@ -67,80 +67,6 @@ frappe.ui.form.on('DocType', {
|
|||
frm.set_value('allow_rename', 0);
|
||||
}
|
||||
},
|
||||
|
||||
naming_rule: function(frm) {
|
||||
// set the "autoname" property based on naming_rule
|
||||
if (frm.doc.naming_rule && !frm.__from_autoname) {
|
||||
|
||||
// flag to avoid recursion
|
||||
frm.__from_naming_rule = true;
|
||||
|
||||
if (frm.doc.naming_rule=='Set by user') {
|
||||
frm.set_value('autoname', 'Prompt');
|
||||
} else if (frm.doc.naming_rule === 'Autoincrement') {
|
||||
frm.set_value('autoname', 'autoincrement');
|
||||
// set allow rename to be false when using autoincrement
|
||||
frm.set_value('allow_rename', 0);
|
||||
} else if (frm.doc.naming_rule=='By fieldname') {
|
||||
frm.set_value('autoname', 'field:');
|
||||
} else if (frm.doc.naming_rule=='By "Naming Series" field') {
|
||||
frm.set_value('autoname', 'naming_series:');
|
||||
} else if (frm.doc.naming_rule=='Expression') {
|
||||
frm.set_value('autoname', 'format:');
|
||||
} else if (frm.doc.naming_rule=='Expression (old style)') {
|
||||
// pass
|
||||
} else if (frm.doc.naming_rule=='Random') {
|
||||
frm.set_value('autoname', 'hash');
|
||||
}
|
||||
setTimeout(() =>frm.__from_naming_rule = false, 500);
|
||||
|
||||
frm.events.set_naming_rule_description(frm);
|
||||
}
|
||||
|
||||
},
|
||||
|
||||
set_naming_rule_description(frm) {
|
||||
let naming_rule_description = {
|
||||
'Set by user': '',
|
||||
'Autoincrement': 'Uses Auto Increment feature of database.<br><b>WARNING: After using this option, any other naming option will not be accessible.</b>',
|
||||
'By fieldname': 'Format: <code>field:[fieldname]</code>. Valid fieldname must exist',
|
||||
'By "Naming Series" field': 'Format: <code>naming_series:[fieldname]</code>. Fieldname called <code>naming_series</code> must exist',
|
||||
'Expression': 'Format: <code>format:EXAMPLE-{MM}morewords{fieldname1}-{fieldname2}-{#####}</code> - Replace all braced words (fieldnames, date words (DD, MM, YY), series) with their value. Outside braces, any characters can be used.',
|
||||
'Expression (old style)': 'Format: <code>EXAMPLE-.#####</code> Series by prefix (separated by a dot)',
|
||||
'Random': '',
|
||||
'By script': ''
|
||||
};
|
||||
|
||||
if (frm.doc.naming_rule) {
|
||||
frm.get_field('autoname').set_description(naming_rule_description[frm.doc.naming_rule]);
|
||||
}
|
||||
},
|
||||
|
||||
autoname: function(frm) {
|
||||
// set naming_rule based on autoname (for old doctypes where its not been set)
|
||||
if (frm.doc.autoname && !frm.doc.naming_rule && !frm.__from_naming_rule) {
|
||||
// flag to avoid recursion
|
||||
frm.__from_autoname = true;
|
||||
if (frm.doc.autoname.toLowerCase() === 'prompt') {
|
||||
frm.set_value('naming_rule', 'Set by user');
|
||||
} else if (frm.doc.autoname.toLowerCase() === 'autoincrement') {
|
||||
frm.set_value('naming_rule', 'Autoincrement');
|
||||
} else if (frm.doc.autoname.startsWith('field:')) {
|
||||
frm.set_value('naming_rule', 'By fieldname');
|
||||
} else if (frm.doc.autoname.startsWith('naming_series:')) {
|
||||
frm.set_value('naming_rule', 'By "Naming Series" field');
|
||||
} else if (frm.doc.autoname.startsWith('format:')) {
|
||||
frm.set_value('naming_rule', 'Expression');
|
||||
} else if (frm.doc.autoname.toLowerCase() === 'hash') {
|
||||
frm.set_value('naming_rule', 'Random');
|
||||
} else {
|
||||
frm.set_value('naming_rule', 'Expression (old style)');
|
||||
}
|
||||
setTimeout(() => frm.__from_autoname = false, 500);
|
||||
}
|
||||
|
||||
frm.set_df_property('fields', 'reqd', frm.doc.autoname !== 'Prompt');
|
||||
},
|
||||
});
|
||||
|
||||
frappe.ui.form.on("DocField", {
|
||||
|
|
|
|||
|
|
@ -208,7 +208,7 @@
|
|||
"label": "Naming"
|
||||
},
|
||||
{
|
||||
"description": "Naming Options:\n<ol><li><b>field:[fieldname]</b> - By Field</li><li><b>autoincrement</b> - Uses Databases' Auto Increment feature</li><li><b>naming_series:</b> - By Naming Series (field called naming_series must be present</li><li><b>Prompt</b> - Prompt user for a name</li><li><b>[series]</b> - Series by prefix (separated by a dot); for example PRE.#####</li>\n<li><b>format:EXAMPLE-{MM}morewords{fieldname1}-{fieldname2}-{#####}</b> - Replace all braced words (fieldnames, date words (DD, MM, YY), series) with their value. Outside braces, any characters can be used.</li></ol>",
|
||||
"description": "Naming Options:\n<ol><li><b>field:[fieldname]</b> - By Field</li><li><b>autoincrement</b> - Uses Databases' Auto Increment feature</li><li><b>naming_series:</b> - By Naming Series (field called naming_series must be present)</li><li><b>Prompt</b> - Prompt user for a name</li><li><b>[series]</b> - Series by prefix (separated by a dot); for example PRE.#####</li>\n<li><b>format:EXAMPLE-{MM}morewords{fieldname1}-{fieldname2}-{#####}</b> - Replace all braced words (fieldnames, date words (DD, MM, YY), series) with their value. Outside braces, any characters can be used.</li></ol>",
|
||||
"fieldname": "autoname",
|
||||
"fieldtype": "Data",
|
||||
"label": "Auto Name",
|
||||
|
|
|
|||
|
|
@ -817,10 +817,8 @@ class DocType(Document):
|
|||
self.autoname != "autoincrement" and doc_before_save.autoname == "autoincrement"
|
||||
):
|
||||
frappe.throw(_("Cannot change to/from Autoincrement naming rule"))
|
||||
|
||||
else:
|
||||
if self.autoname == "autoincrement":
|
||||
self.allow_rename = 0
|
||||
if self.autoname == "autoincrement":
|
||||
self.allow_rename = 0
|
||||
|
||||
def validate_name(self, name=None):
|
||||
if not name:
|
||||
|
|
@ -865,8 +863,13 @@ def validate_series(dt, autoname=None, name=None):
|
|||
|
||||
if not autoname and dt.get("fields", {"fieldname": "naming_series"}):
|
||||
dt.autoname = "naming_series:"
|
||||
elif dt.autoname == "naming_series:" and not dt.get("fields", {"fieldname": "naming_series"}):
|
||||
frappe.throw(_("Invalid fieldname '{0}' in autoname").format(dt.autoname))
|
||||
elif dt.autoname.startswith("naming_series:"):
|
||||
fieldname = dt.autoname.split("naming_series:")[0] or "naming_series"
|
||||
if not dt.get("fields", {"fieldname": fieldname}):
|
||||
frappe.throw(
|
||||
_("Fieldname called {0} must exist to enable autonaming").format(frappe.bold(fieldname)),
|
||||
title=_("Field Missing"),
|
||||
)
|
||||
|
||||
# validate field name if autoname field:fieldname is used
|
||||
# Create unique index on autoname field automatically.
|
||||
|
|
|
|||
|
|
@ -24,6 +24,7 @@
|
|||
"fields_section_break",
|
||||
"fields",
|
||||
"naming_section",
|
||||
"naming_rule",
|
||||
"autoname",
|
||||
"view_settings_section",
|
||||
"title_field",
|
||||
|
|
@ -50,6 +51,13 @@
|
|||
"sort_order"
|
||||
],
|
||||
"fields": [
|
||||
{
|
||||
"fieldname": "naming_rule",
|
||||
"fieldtype": "Select",
|
||||
"label": "Naming Rule",
|
||||
"length": 40,
|
||||
"options": "\nSet by user\nAutoincrement\nBy fieldname\nBy \"Naming Series\" field\nExpression\nExpression (old style)\nRandom\nBy script"
|
||||
},
|
||||
{
|
||||
"fieldname": "doc_type",
|
||||
"fieldtype": "Link",
|
||||
|
|
@ -279,7 +287,7 @@
|
|||
"label": "Naming"
|
||||
},
|
||||
{
|
||||
"description": "Naming Options:\n<ol><li><b>field:[fieldname]</b> - By Field</li><li><b>naming_series:</b> - By Naming Series (field called naming_series must be present</li><li><b>Prompt</b> - Prompt user for a name</li><li><b>[series]</b> - Series by prefix (separated by a dot); for example PRE.#####</li>\n<li><b>format:EXAMPLE-{MM}morewords{fieldname1}-{fieldname2}-{#####}</b> - Replace all braced words (fieldnames, date words (DD, MM, YY), series) with their value. Outside braces, any characters can be used.</li></ol>",
|
||||
"description": "Naming Options:\n<ol><li><b>field:[fieldname]</b> - By Field</li><li><b>autoincrement</b> - Uses Databases' Auto Increment feature</li><li><b>naming_series:</b> - By Naming Series (field called naming_series must be present)</li><li><b>Prompt</b> - Prompt user for a name</li><li><b>[series]</b> - Series by prefix (separated by a dot); for example PRE.#####</li>\n<li><b>format:EXAMPLE-{MM}morewords{fieldname1}-{fieldname2}-{#####}</b> - Replace all braced words (fieldnames, date words (DD, MM, YY), series) with their value. Outside braces, any characters can be used.</li></ol>",
|
||||
"fieldname": "autoname",
|
||||
"fieldtype": "Data",
|
||||
"label": "Auto Name"
|
||||
|
|
|
|||
|
|
@ -20,4 +20,92 @@ frappe.model.DocTypeController = class DocTypeController extends frappe.ui.form.
|
|||
__("Number of attachment fields are more than {}, limit updated to {}.", [label, no_of_attach_fields]));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
naming_rule() {
|
||||
// set the "autoname" property based on naming_rule
|
||||
if (this.frm.doc.naming_rule && !this.frm.__from_autoname) {
|
||||
|
||||
// flag to avoid recursion
|
||||
this.frm.__from_naming_rule = true;
|
||||
|
||||
switch (this.frm.doc.naming_rule) {
|
||||
case "Set by user":
|
||||
this.frm.set_value("autoname", "Prompt");
|
||||
break;
|
||||
case "Autoincrement":
|
||||
this.frm.set_value("autoname", "autoincrement");
|
||||
break;
|
||||
case "By fieldname":
|
||||
this.frm.set_value("autoname", "field:");
|
||||
break;
|
||||
case 'By "Naming Series" field':
|
||||
this.frm.set_value("autoname", "naming_series:");
|
||||
break;
|
||||
case "Expression":
|
||||
this.frm.set_value("autoname", "format:");
|
||||
break;
|
||||
case "Expression (old style)":
|
||||
break;
|
||||
case "Random":
|
||||
this.frm.set_value("autoname", "hash");
|
||||
break;
|
||||
}
|
||||
setTimeout(() =>this.frm.__from_naming_rule = false, 500);
|
||||
|
||||
this.set_naming_rule_description();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
set_naming_rule_description() {
|
||||
let naming_rule_description = {
|
||||
'Set by user': '',
|
||||
'Autoincrement': 'Uses Auto Increment feature of database.<br><b>WARNING: After using this option, any other naming option will not be accessible.</b>',
|
||||
'By fieldname': 'Format: <code>field:[fieldname]</code>. Valid fieldname must exist',
|
||||
'By "Naming Series" field': 'Format: <code>naming_series:[fieldname]</code>. Default fieldname is <code>naming_series</code>',
|
||||
'Expression': 'Format: <code>format:EXAMPLE-{MM}morewords{fieldname1}-{fieldname2}-{#####}</code> - Replace all braced words (fieldnames, date words (DD, MM, YY), series) with their value. Outside braces, any characters can be used.',
|
||||
'Expression (old style)': 'Format: <code>EXAMPLE-.#####</code> Series by prefix (separated by a dot)',
|
||||
'Random': '',
|
||||
'By script': ''
|
||||
};
|
||||
|
||||
if (this.frm.doc.naming_rule) {
|
||||
this.frm.get_field('autoname').set_description(naming_rule_description[this.frm.doc.naming_rule]);
|
||||
}
|
||||
}
|
||||
|
||||
autoname() {
|
||||
// set naming_rule based on autoname (for old doctypes where its not been set)
|
||||
if (this.frm.doc.autoname && !this.frm.doc.naming_rule && !this.frm.__from_naming_rule) {
|
||||
// flag to avoid recursion
|
||||
this.frm.__from_autoname = true;
|
||||
const autoname = this.frm.doc.autoname.toLowerCase();
|
||||
|
||||
switch (autoname) {
|
||||
case 'prompt':
|
||||
this.frm.set_value('naming_rule', 'Set by user');
|
||||
break;
|
||||
case 'autoincrement':
|
||||
this.frm.set_value('naming_rule', 'Autoincrement');
|
||||
break;
|
||||
case (autoname.startsWith('field:')):
|
||||
this.frm.set_value('naming_rule', 'By fieldname');
|
||||
break;
|
||||
case (autoname.startsWith('naming_series:')):
|
||||
this.frm.set_value('naming_rule', 'By "Naming Series" field');
|
||||
break;
|
||||
case (autoname.startsWith('format:')):
|
||||
this.frm.set_value('naming_rule', 'Expression');
|
||||
break;
|
||||
case 'hash':
|
||||
this.frm.set_value('naming_rule', 'Random');
|
||||
break;
|
||||
default:
|
||||
this.frm.set_value('naming_rule', 'Expression (old style)');
|
||||
}
|
||||
setTimeout(() => this.frm.__from_autoname = false, 500);
|
||||
}
|
||||
|
||||
this.frm.set_df_property('fields', 'reqd', this.frm.doc.autoname !== 'Prompt');
|
||||
}
|
||||
};
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue