Javascript 流星自动形成中的独特场
我有一个Meteor AutoForm集合模式,带有以下字段,我正在尝试使其唯一。它不允许在相同的情况下使用相同的值,但当我更改值的大小写时,会插入值,因此如何防止插入不同大小写的重复值 像Javascript 流星自动形成中的独特场,javascript,meteor,meteor-autoform,simple-schema,Javascript,Meteor,Meteor Autoform,Simple Schema,我有一个Meteor AutoForm集合模式,带有以下字段,我正在尝试使其唯一。它不允许在相同的情况下使用相同的值,但当我更改值的大小写时,会插入值,因此如何防止插入不同大小写的重复值 像Test,Test,Test都有相同的拼写,所以不应该插入 我试过这个: Schemas.Organisation = new SimpleSchema({ company: { type: String, max: 200, unique: true,
Test
,Test
,Test
都有相同的拼写,所以不应该插入
我试过这个:
Schemas.Organisation = new SimpleSchema({
company: {
type: String,
max: 200,
unique: true,
autoValue: function () {
if (this.isSet && typeof this.value === "string") {
return this.value.toLowerCase();
}
},
autoform:{
label: false,
afFieldInput: {
placeholder: "Enter Company Name",
}
}
}
})
但它不是让我插入重复值,而是在保存到db时转换为所有小写。那么,如何将该值保存为用户输入的值,但该值不应具有相同的拼写?这可以通过使用自定义客户端验证来实现。如果您不想向每个客户发布您的
组织的所有文档,您可以使用,例如:
Organisations = new Mongo.Collection("organisations");
Organisations.attachSchema(new SimpleSchema({
company: {
type: String,
max: 200,
unique: true,
custom: function() {
if (Meteor.isClient && this.isSet) {
Meteor.call("isCompanyUnique", this.value, function(error, result) {
if (!result) {
Organisations.simpleSchema().namedContext("insertCompanyForm").addInvalidKeys([{
name: "company",
type: "notUnique"
}]);
}
});
}
},
autoValue: function() {
if (this.isSet && typeof this.value === "string") {
return this.value.toLowerCase();
}
},
autoform: {
label: false,
afFieldInput: {
placeholder: "Enter Company Name",
}
}
}
}));
{{>QuickFormCollection=“Organizations”id=“insertCompanyForm”type=“insert”}
这里有一个。我在寻找针对_id的自定义验证时偶然发现了这一点。我尝试了上述方法,似乎在验证错误发生后,我再次点击提交多次,我似乎遇到了错误。服务器日志显示:调用方法“/aggrouter\u aaaContext\u template\u collection/insert”时出现异常错误:已定义了一个名为“isagrunique”的方法我已提出了一个有关该方法的新问题
if (Meteor.isServer) {
Meteor.methods({
isCompanyUnique: function(companyName) {
return Organisations.find({
company: companyName.toUpperCase()
}).count() === 0;
}
});
}
<body>
{{> quickForm collection="Organisations" id="insertCompanyForm" type="insert"}}
</body>