我有一个自定义模块,其中有一个电子邮件字段。现在,如果电子邮件已经在数据库中,我想停止用户。
我想在保存按钮上停止用户并显示错误。就像当必填字段为空时。
我试图获得一些帮助,但无法理解。
[注意:发布此信息后,我意识到您正在使用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); }) }); }, })
显然,这不是一个可以正常工作的示例,但它应该可以助您一臂之力。希望这会有所帮助!