将自定义输入字段添加到Meteor中的AutoForm

时间:2015-11-05 22:49:41

标签: javascript meteor meteor-autoform pickadate simple-schema

  {{#autoForm schema="schema" id="submitoffer" type="method" meteormethod="submitoffer"}}
      {{> afQuickField name="startLocation"}}
      <input id="date" type="text" class="form-control datepicker">
      <input id="departureTime" type="text"  class="form-control timepicker">
      <input id="returnTime" type="text"  class="form-control timepicker">
      {{> afQuickField name="seats" type="number"}}
      <button type="submit" class="btn btn-primary">Offer lift</button>
  {{/autoForm}}

我希望能够使用date,departureTime和returnTime输入(这是pickadate.js的实现。但是当我将表单提交给服务器时,这些输入不会作为表单的一部分被选中。如何在其中输入输入以及使用autoform提交它们?

1 个答案:

答案 0 :(得分:1)

您可以使用afFieldInput元素并在架构中设置class属性。

例如:

<body>
    {{#autoForm collection="Offers" id="submitoffer" type="insert"}}
        {{> afQuickField name="startLocation"}}
        {{> afFieldInput name="date"}}
        {{> afFieldInput name="departureTime"}}
        {{> afFieldInput name="returnTime"}}
        {{> afQuickField name="seats"}}
        <button type="submit" class="btn btn-primary">Offer lift</button>
    {{/autoForm}}
</body>
if (Meteor.isClient) {
    Template.body.onRendered(function () {
        $('.datepicker').pickadate();
        $('.timepicker').pickatime();
    });
}

Offers = new Mongo.Collection("offers");

Offers.attachSchema(new SimpleSchema({
    date: {
        type: String,
        label: "Date",
        autoform: {
            afFieldInput: {
                class: "datepicker"
            }
        }
    },
    departureTime: {
        type: String,
        label: "Departure Time",
        autoform: {
            afFieldInput: {
                class: "timepicker"
            }
        }
    },
    returnTime: {
        type: String,
        label: "Return Time",
        autoform: {
            afFieldInput: {
                class: "timepicker"
            }
        }
    },
    seats: {
        type: Number,
        label: "Seats"
    },
    startLocation: {
        type: String,
        label: "Start Location"
    }
}));

请注意:上面给出的示例使用字段date的类型字符串。我强烈建议将date值存储为JavaScript Date个对象。您可能希望使用before hook将字符串转换为日期对象。