【问题标题】:Using Autoform package with Meteor Blaze but form won't submit. No errors将 Autoform 包与 Meteor Blaze 一起使用,但表单不会提交。没有错误
【发布时间】:2020-11-13 09:23:22
【问题描述】:

我正在使用 autoForm 生成表单。我有一个使用 simpl-schema 包设置的模式。当我单击表单上的提交时,什么也没有发生。我没有收到错误消息,并且我的数据库中没有新条目。该架构似乎正在工作,因为如果输入错误,表单上会显示字符最小限制。

我对 autoForm 将提交路由到哪个方法感到困惑。据我了解,它应该会自动执行,我不需要创建事件侦听器来处理提交。

我的代码在下面

食谱.html

<template name="insertRecipeForm">
  {{> quickForm collection="Recipes" id="insertRecipeForm" type="insert"}} 
</template>

recipe.js

export const Recipes = new Mongo.Collection('recipes');
const Schemas = {};
Schemas.Recipe = new SimpleSchema({
  name: {
    type: String,
    min: 1},
  instructions: {
    type: String,
    min: 5,
    autoform: {
      afFieldInput: {
        type: "textarea",
        rows: 2,
        class: "foo"
      }
    }
  },
  owner: {
    type: String,
    min: 1,
    autoform: {
      type: "hidden"
    }
  },
  username: {
    type: String,
    min: 1,
    autoform: {
      type: "hidden"
    }
  }},
    { tracker: Tracker })

Recipes.attachSchema(Schemas.Recipe)

我的 recipe.js 文件中有一个旧的“recipes.insert”方法。这会干扰 autoForm 吗?

【问题讨论】:

  • 我提供了答案,有帮助吗?
  • 如果对你有帮助,请标记为已接受,以便其他有类似问题的人有方向。

标签: javascript meteor meteor-autoform


【解决方案1】:

insert 类型仅在客户端插入。如果要将值传递给流星方法,则需要设置type="method" meteormethod="recipes.insert"

或者,您可以使用 type="normal" 并拦截 submit 事件以使用您的自定义提交:

Template.myTemplate.events({
  'submit #insertRecipeForm' (event) {
    event.preventDefault() // cancel submission
    const { insertDoc } = AutoForm.getFormValues('insertRecipeForm')

    Meteor.call('recipes.insert', insertDoc)
  }
})

见:https://github.com/Meteor-Community-Packages/meteor-autoform#non-collection-forms

【讨论】:

    猜你喜欢
    • 2014-06-03
    • 2016-03-30
    • 2014-09-21
    • 1970-01-01
    • 2015-07-05
    • 2015-11-18
    • 2015-11-29
    • 2015-09-15
    • 2017-10-05
    相关资源
    最近更新 更多