首页 > 解决方案 > 如何阻止用户在默认保存时输入重复记录

问题描述

我有一个自定义模块,其中有一个电子邮件字段。现在,如果电子邮件已经在数据库中,我想停止用户。

我想在保存按钮上停止用户并显示错误。就像必填字段为空时一样。

我试图获得一些帮助,但无法理解。

标签: sugarcrmsuitecrm

解决方案


注意:发布此消息后,我意识到您正在使用 suitecrm,此答案将不适用于此答案,但如果使用 Sugar 的任何人有此问题,我将保留它。

有几种方法可以做到这一点,所以我会尽力按照我推荐的顺序遍历它们。如果您使用的是 Sugar post 7.0.0 版本,这将适用。

1)第一种路线是手动创建电子邮件地址关系。这种方法将使用开箱即用的功能,这将确保您的系统仅跟踪单个电子邮件地址。如果这能满足您的需求,您可以查看这篇食谱文章,如果您有任何问题,请告诉我:

https://support.sugarcrm.com/Documentation/Sugar_Developer/Sugar_Developer_Guide_9.2/Cookbook/Adding_the_Email_Field_to_a_Bean/

2) 使用自定义字段的第二种方法是使用字段验证。可以在此处找到有关字段验证的文档:

https://support.sugarcrm.com/Documentation/Sugar_Developer/Sugar_Developer_Guide_9.2/Cookbook/Adding_Field_Validation_to_the_Record_View/index.html

我要关注的代码示例是:

https://support.sugarcrm.com/Documentation/Sugar_Developer/Sugar_Developer_Guide_9.2/Cookbook/Adding_Field_Validation_to_the_Record_View/#Method_1_Extending_the_RecordView_and_CreateView_Controllers

对于你的例子,我想你会做这样的事情:

为您的错误消息创建语言键:

./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);
            })
        });
    },
})

显然,这不是一个完全有效的示例,但它应该让您大部分时间到达那里。希望这可以帮助!


推荐阅读