Custom validation fails on multiple subsequent attempts


#1

Hello,

I’m using a custom validation on _id field as below. First I enter an ID and hit submit, it gets persisted in the DB. Next time, I reuse the same ID and try to submit, I get a validation error. I hit the submit button again, and I get a weird internal server error being thrown in the chrome logs. The validation error vanishes and feels as if the form is submitted successfully, but in reality it isn’t. I’m not able to figure out why this happens on the second try onwards. Typically, the validation error must show up again and again, how many ever times I hit submit. What am I doing wrong?

Schema:

"_id":{
        type:String,
        label: "Template Name",
        unique: true,
        autoform:{
            textHelp:"Unique name to identify the template"
        },
        custom: function() {
            if (Meteor.isClient && this.isSet) {
                Meteor.call("isAggrUnique", this.value, function(error, result) {
                    if (!result) {
                        aggrRouter_aaaContext_template_collection.simpleSchema().namedContext("aggrRouter_aaaContext_template_form").addInvalidKeys([{
                            name: "_id",
                            type: "notUnique"
                        }]);
                    }
                });

            }
            if (Meteor.isServer) {
                Meteor.methods({
                    isAggrUnique: function(id) {
                        return aggrRouter_aaaContext_template_collection.find({
                            _id: id.toUpperCase()
                        }).count() === 0;
                    }
                });
            }
        }
    },

Upon multiple submits, Chrome dev logs show:

aldeed_autoform.js?hash=9676200…:7782 errorClass {error: 500, reason: "Internal server error", details: undefined, message: "Internal server error [500]", errorType: "Meteor.Error"}

Meteor server logs show:

I20160720-17:23:11.392(5.5)? Exception while invoking method '/aggrRouter_aaaContext_template_collection/insert' Error: A method named 'isAggrUnique' is already defined
I20160720-17:23:11.393(5.5)?     at packages/ddp-server/livedata_server.js:1548:15
I20160720-17:23:11.393(5.5)?     at Function._.each._.forEach (packages/underscore/underscore.js:113:1)
I20160720-17:23:11.393(5.5)?     at Server.methods (packages/ddp-server/livedata_server.js:1544:7)
I20160720-17:23:11.394(5.5)?     at Object.custom (both/collections/aaa_context_schema.js:23:24)
I20160720-17:23:11.394(5.5)?     at packages/aldeed_simple-schema/simple-schema-validation.js:170:1
I20160720-17:23:11.394(5.5)?     at packages/underscore/underscore.js:221:1
I20160720-17:23:11.394(5.5)?     at _.each._.forEach (packages/underscore/underscore.js:108:1)
I20160720-17:23:11.395(5.5)?     at Function._.every._.all (packages/underscore/underscore.js:220:1)
I20160720-17:23:11.395(5.5)?     at validate (packages/aldeed_simple-schema/simple-schema-validation.js:169:1)
I20160720-17:23:11.395(5.5)?     at checkObj (packages/aldeed_simple-schema/simple-schema-validation.js:227:1)

Why is it saying that I’ve defined isAggrUnique more than once?