带有collection2的Meteor Autoform包不提交表单

时间:2021-08-01 17:00:42

I am using using collection2 and autoform with one level nested schemas

我正在使用collection2和autoform与一个级别的嵌套模式

Node = new Meteor.Collection('node',{
schema: new SimpleSchema({
       content: {
           type: String,
           label: "Content",
           max: 200
       }
       created: {
           type: Date,
           label: "Created Date"
       },
       modified: {
           type: Date,
           label: "Modified Date"
       }
   })
});

NodeMeta = new Meteor.Collection('Node_meta',{
schema: new SimpleSchema({
    desc:{
        type:String,
        max:200,
        label:"Description",
        optional:true
    }
})

});

Schema = {};

Schema.nodeWithMeta= new SimpleSchema({
  Node: {
     type: Node.simpleSchema()
  },
  Meta: {
     type: NodeMeta.simpleSchema()
  }
});


{{#autoForm schema=schema id="nodeForm" type="method" meteormethod="nodecreate"}}
  {{> afFieldInput name='Node.content' rows=8 }}
  {{> afFieldInput name='Meta.desc' rows=8}}    
  <button type="submit" class="btn btn-primary btn-submit">Create</button>
{{/autoForm}}

Template.nodeCreate.helpers({
  schema: function() {
    return Schema.nodeWithMeta;
  }
});

It does not call server method. I tried autoform onSubmit hook as well as Meteors inbuilt templates 'submit' event handler. If I use jQuery onsubmit event handler, it registers the event. I cannot use jQuery for this purpose since autoform has to validate the inputs.

它不调用服务器方法。我尝试了autoform onSubmit hook以及Meteors内置模板'submit'事件处理程序。如果我使用jQuery onsubmit事件处理程序,它会注册该事件。我不能为此目的使用jQuery,因为autoform必须验证输入。

2 个解决方案

#1


3  

Since created and modified are required fields, it's most likely not submitting because those fields are missing from the form, which means the form is invalid. There are actually a few different ways you can solve this:

由于创建和修改是必填字段,因此很可能无法提交,因为表单中缺少这些字段,这意味着表单无效。实际上有几种不同的方法可以解决这个问题:

  1. Make them optional (probably not what you want)
  2. 让它们成为可选的(可能不是你想要的)

  3. Use a different schema, one without those two fields, for the schema attribute of the autoform.
  4. 对于autoform的schema属性,使用不同的模式(没有这两个字段的模式)。

  5. Add a "before method" hook that sets those two fields in the submitted doc.
  6. 添加一个“before method”钩子,用于在提交的doc中设置这两个字段。

  7. Use autoValue to generate the values for those two fields.
  8. 使用autoValue为这两个字段生成值。

Since created and modified dates are fairly easy to do with autoValue, I would do it that way. Something like this:

由于使用autoValue创建和修改日期相当容易,我会这样做。像这样的东西:

created: {
    type: Date,
    label: "Created Date",
    autoValue: function () {
        if (this.isInsert) {
          return new Date;
        } else {
          this.unset();
        }
    }
},
modified: {
    type: Date,
    label: "Modified Date",
    autoValue: function () {
        if (this.isInsert) {
          this.unset();
        } else {
          return new Date;
        }
    }
}

Also, to help figure out issues like this more easily while developing, I recommend enabling debug mode.

另外,为了在开发过程中更容易找出这样的问题,我建议启用调试模式。

#2


2  

Have you allowed inserts/updates on your collection? See http://docs.meteor.com/#dataandsecurity.

您是否允许在您的收藏中插入/更新?请参见http://docs.meteor.com/#dataandsecurity。

I bet if you run the two commands below, it'll work.

我打赌如果你运行下面的两个命令,它会工作。

meteor add insecure
meteor add autopublish 

Now try your submit.

现在试试你的提交。

If it does work, turn autopublish and insecure back off

如果它确实有效,请关闭自动发布和不安全状态

meteor remove insecure
meteor remove autopublish

Then write your allow methods, e.g.

然后编写允许方法,例如

Node.allow({
  insert: function (userId, nodeDoc) {
    // write some logic to allow the insert 
    return true;
  }
});

#1


3  

Since created and modified are required fields, it's most likely not submitting because those fields are missing from the form, which means the form is invalid. There are actually a few different ways you can solve this:

由于创建和修改是必填字段,因此很可能无法提交,因为表单中缺少这些字段,这意味着表单无效。实际上有几种不同的方法可以解决这个问题:

  1. Make them optional (probably not what you want)
  2. 让它们成为可选的(可能不是你想要的)

  3. Use a different schema, one without those two fields, for the schema attribute of the autoform.
  4. 对于autoform的schema属性,使用不同的模式(没有这两个字段的模式)。

  5. Add a "before method" hook that sets those two fields in the submitted doc.
  6. 添加一个“before method”钩子,用于在提交的doc中设置这两个字段。

  7. Use autoValue to generate the values for those two fields.
  8. 使用autoValue为这两个字段生成值。

Since created and modified dates are fairly easy to do with autoValue, I would do it that way. Something like this:

由于使用autoValue创建和修改日期相当容易,我会这样做。像这样的东西:

created: {
    type: Date,
    label: "Created Date",
    autoValue: function () {
        if (this.isInsert) {
          return new Date;
        } else {
          this.unset();
        }
    }
},
modified: {
    type: Date,
    label: "Modified Date",
    autoValue: function () {
        if (this.isInsert) {
          this.unset();
        } else {
          return new Date;
        }
    }
}

Also, to help figure out issues like this more easily while developing, I recommend enabling debug mode.

另外,为了在开发过程中更容易找出这样的问题,我建议启用调试模式。

#2


2  

Have you allowed inserts/updates on your collection? See http://docs.meteor.com/#dataandsecurity.

您是否允许在您的收藏中插入/更新?请参见http://docs.meteor.com/#dataandsecurity。

I bet if you run the two commands below, it'll work.

我打赌如果你运行下面的两个命令,它会工作。

meteor add insecure
meteor add autopublish 

Now try your submit.

现在试试你的提交。

If it does work, turn autopublish and insecure back off

如果它确实有效,请关闭自动发布和不安全状态

meteor remove insecure
meteor remove autopublish

Then write your allow methods, e.g.

然后编写允许方法,例如

Node.allow({
  insert: function (userId, nodeDoc) {
    // write some logic to allow the insert 
    return true;
  }
});