sugarcrm - 如何阻止用户在默认保存时输入重复记录
问题描述
我有一个自定义模块,其中有一个电子邮件字段。现在,如果电子邮件已经在数据库中,我想停止用户。
我想在保存按钮上停止用户并显示错误。就像必填字段为空时一样。
我试图获得一些帮助,但无法理解。
解决方案
注意:发布此消息后,我意识到您正在使用 suitecrm,此答案将不适用于此答案,但如果使用 Sugar 的任何人有此问题,我将保留它。
有几种方法可以做到这一点,所以我会尽力按照我推荐的顺序遍历它们。如果您使用的是 Sugar post 7.0.0 版本,这将适用。
1)第一种路线是手动创建电子邮件地址关系。这种方法将使用开箱即用的功能,这将确保您的系统仅跟踪单个电子邮件地址。如果这能满足您的需求,您可以查看这篇食谱文章,如果您有任何问题,请告诉我:
2) 使用自定义字段的第二种方法是使用字段验证。可以在此处找到有关字段验证的文档:
我要关注的代码示例是:
对于你的例子,我想你会做这样的事情:
为您的错误消息创建语言键:
./custom/Extension/application/Ext/Language/en_us.error_email_exists_message.php
<?php
$app_strings['ERROR_EMAIL_EXISTS_MESSAGE'] = 'This email already exists.';
为记录创建创建一个自定义控制器(您可能还想在您的 record.js 中执行此操作):
./custom/modules//clients/base/views/create/create.js
({
extendsFrom: 'RecordView',
initialize: function (options) {
this._super('initialize', [options]);
//reference your language key here
app.error.errorName2Keys['email_exists'] = 'ERROR_EMAIL_EXISTS_MESSAGE';
//add validation tasks
this.model.addValidationTask('check_email', _.bind(this._doValidateEmail, this));
},
_doValidateEmail: function(fields, errors, callback) {
var emailAddress = this.model.get('your_email_field');
//this may take some time so lets give the user an alert message
app.alert.show('email-check', {
level: 'process',
title: 'Checking for existing email address...'
});
//make an api call to a custom (or stock) endpoint of your choosing to see if the email exists
app.api.call('read', app.api.buildURL("your_custom_endpoint/"+emailAddress), {}, {
success: _.bind(function (response) {
//dismiss the alert
app.alert.dismiss('email-check');
//analyze your response here
if (response == '<email exists>') {
errors['your_email_field'] = errors['your_email_field'] || {};
errors['your_email_field'].email_exists = true;
}
callback(null, fields, errors);
}, this),
error: _.bind(function (response) {
//dismiss the alert
app.alert.dismiss('email-check');
//throw an error alert
app.alert.show('email-check-error', {
level: 'error',
messages: "There was an error!",
autoClose: false
});
callback(null, fields, errors);
})
});
},
})
显然,这不是一个完全有效的示例,但它应该让您大部分时间到达那里。希望这可以帮助!
推荐阅读
- excel - 打开 Office 应用程序错误:Microsoft.Office.Interop.Excel / Microsoft.Office.Interop.Word
- node.js - 从 /usr/local/bin/node 卸载节点
- google-chrome - 如何在 2020 年禁用谷歌浏览器输入字段自动填充
- javascript - 使用信号 r 和 javascript 构建动态网页的最佳方法是什么?
- pytorch - 调整火炬张量通道的大小
- r - 跨多个组对同一响应变量执行多个 t 检验
- vue.js - 如何使用 vue-konva 在屏幕上的对象上调用 hide()?
- python - 根据元素类型切片 pandas 系列
- mongodb - 从 mongo db 中删除除一个重复项之外的所有重复项
- javascript - 为什么我无法在这种情况下修改“this”?