Merge pull request #14943 from resilient-tech/fix-validate-link

This commit is contained in:
Suraj Shetty 2021-11-11 17:28:32 +05:30 committed by GitHub
commit df862ddc45
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 45 additions and 24 deletions

View file

@ -49,19 +49,19 @@ context('Control Link', () => {
it('should unset invalid value', () => {
get_dialog_with_link().as('dialog');
cy.intercept('GET', '/api/method/frappe.client.get_value*').as('get_value');
cy.intercept('POST', '/api/method/frappe.client.validate_link*').as('validate_link');
cy.get('.frappe-control[data-fieldname=link] input')
.type('invalid value', { delay: 100 })
.blur();
cy.wait('@get_value');
cy.wait('@validate_link');
cy.get('.frappe-control[data-fieldname=link] input').should('have.value', '');
});
it('should route to form on arrow click', () => {
get_dialog_with_link().as('dialog');
cy.intercept('GET', '/api/method/frappe.client.get_value*').as('get_value');
cy.intercept('POST', '/api/method/frappe.client.validate_link*').as('validate_link');
cy.intercept('POST', '/api/method/frappe.desk.search.search_link').as('search_link');
cy.get('@todos').then(todos => {
@ -69,7 +69,7 @@ context('Control Link', () => {
cy.get('@input').focus();
cy.wait('@search_link');
cy.get('@input').type(todos[0]).blur();
cy.wait('@get_value');
cy.wait('@validate_link');
cy.get('@input').focus();
cy.findByTitle('Open Link')
.should('be.visible')

View file

@ -405,3 +405,23 @@ def is_document_amended(doctype, docname):
pass
return False
@frappe.whitelist()
def validate_link(doctype: str, docname: str):
if not isinstance(doctype, str):
frappe.throw(_("DocType must be a string"))
if not isinstance(docname, str):
frappe.throw(_("Document Name must be a string"))
if doctype != "DocType" and not (
frappe.has_permission(doctype, "select")
or frappe.has_permission(doctype, "read")
):
frappe.throw(
_("You do not have Read or Select Permissions for {}")
.format(frappe.bold(doctype)),
frappe.PermissionError
)
return frappe.db.get_value(doctype, docname, cache=True)

View file

@ -454,38 +454,39 @@ frappe.ui.form.ControlLink = class ControlLink extends frappe.ui.form.ControlDat
validate_link_and_fetch(df, options, docname, value) {
if (!value) return;
return new Promise((resolve) => {
return new Promise(async (resolve) => {
const fetch_map = this.fetch_map;
const columns_to_fetch = Object.values(fetch_map);
// if default and no fetch, no need to validate
if ($.isEmptyObject(fetch_map) && df.__default_value === value) {
if (!columns_to_fetch.length && df.__default_value === value) {
return resolve(value);
}
const name = await frappe.xcall("frappe.client.validate_link", {
doctype: options,
docname: value
});
if (!name) return resolve("");
if (!docname || !columns_to_fetch.length) return resolve(name);
frappe.db.get_value(
options,
value,
["name", ...Object.values(fetch_map)],
columns_to_fetch,
(response) => {
if (!response.name) {
return resolve("");
for (const [target_field, source_field] of Object.entries(fetch_map)) {
frappe.model.set_value(
df.parent,
docname,
target_field,
response[source_field],
df.fieldtype,
);
}
if (docname) {
for (const [target_field, source_field] of Object.entries(fetch_map)) {
frappe.model.set_value(
df.parent,
docname,
target_field,
response[source_field],
df.fieldtype,
);
}
}
return resolve(response.name);
}
)
).always(() => resolve(name));
});
}