简体   繁体   English

流星AutoForm中的独特字段

[英]Unique Field in Meteor AutoForm

I have a Meteor AutoForm collection schema with the following field and I am trying to make it unique. 我有一个Meteor AutoForm集合架构与以下字段,我试图使其独特。 It doesn't allow the same value in the same case but when I change the case of the value, value get inserted, so how can I prevent to insert duplicate value with different case? 它在同一情况下不允许相同的值,但是当我更改值的大小写时,会插入值,那么如何防止使用不同的大小写插入重复值?

Like Test , TEST , TesT all having the same spell so it should not get inserted. TestTESTTesT都具有相同的法术,因此不应插入。

I tried this: 我试过这个:

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",
            }
        }
    }
  })

But it is not let me inserted the duplicate value but it converting to all lower case while saving in the db. 但它不是让我插入重复值,而是在保存在db中时转换为全部小写。 So how can I save the value as user entered, but value should not have same spell? 那么如何保存用户输入的值,但值不应该有相同的法术?

This can be achieved by using a custom client-side validation. 这可以通过使用自定义客户端验证来实现。 If you don't want to publish all documents of your Organisation collection to every client, you could use an asynchronous validation approach , for example: 如果您不希望将Organisation集合的所有文档发布到每个客户端,则可以使用异步验证方法 ,例如:

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

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>

Here's a MeteorPad . 这是一个MeteorPad

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM