繁体   English   中英

Meteor AutoForm设置了必填字段

[英]Meteor AutoForm set optional field to be required

我有以下(简化的)SimpleSchema:

EventSchema = new SimpleSchema({
    eventType: {
        type: String
    },
    kicker: {
        type: String
    },
    kicker2: {
        type: String,
        optional: true
    }
});

有了这个,我正在使用AutoForm来生成一个插入表单。 这是它的简化版本:

{{#autoForm schema="EventSchema" type="method" meteormethod="addEvent"}}
    {{> afFieldInput name="eventType" options=getSubstitutionEventTypes type="select-radio-inline"}}

    {{> afFieldInput name="kicker" type="select" options=this}}

    {{> afFieldInput name="kicker2" type="select" options=this}}
{{/autoForm}}

当我在其他不需要输入“ kicker2”的autoForm中使用此架构时,我将该字段设置为可选。 但以上述形式,此字段也是必填字段。 那么,如何覆盖特定格式的字段的可选设置?

我已经尝试过以下方法,但是没有用(HTML中未呈现所需的内容):

{{> afFieldInput name="kicker2" type="select" options=this required="required"}}

提前致谢!

您有一些技巧,可以根据情况使用可选值,一个不错的方法是在函数返回时设置可选值,如下所示:

EventSchema = new SimpleSchema({
    eventType: {
        type: String
    },
    kicker: {
        type: String
    },
    kicker2: {
        type: String,
        optional: function() {
            return (! this.isInsert)
        }
    }
});

因此,它在更新时是可选的,但在插入时不是可选的(您可以使用任何方式对其进行自定义)。 在表单之间具有不同验证规则的另一种方法是,只需为给定表单创建特定的架构,然后使用autoForm(schema=yourSpecificSchema ...而不是autoForm(collection="Meteor.users" 。)不要忘记注册一个帮助器。因此您可以从表单访问架构。有关更多详细信息,请参考官方文档: https : //github.com/aldeed/meteor-autoform#non-collection-forms

暂无
暂无

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

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