本文介绍了Meteor AutoForm 设置可选字段为必填项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我有以下(简化的)SimpleSchema:

I've the following (simplified) SimpleSchema:

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

有了这个,我使用 AutoForm 生成插入表单.这是它的简化版本:

With this I'm using AutoForm to generate a insert form. Here is a simplified version of it:

{{#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}}

由于我在另一个 autoForm 中使用此架构,而不必输入kicker2",因此我将此字段设置为可选.但是在上面提到的表单中,这个字段也是必需的.那么如何覆盖特定表单中字段的可选设置?

As I'm using this Schema in an other autoForm where I don't have to input "kicker2", I've set this field to be optional. But in the form mentioned above, this field is required too. So how can I override the optional setting for a field in a specific form?

我已经尝试了以下方法,但没有奏效(HTML 中未呈现 required ):

I've already tried the following, but it didn't work (required isn't rendered in the HTML):

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

提前致谢!

推荐答案

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

You have some tricks to have an optional value depending on situations, a nice one would be to set the optional value on the return of a function, giving something like this:

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

So it is optional on update but not on insert (you can use whatever means to customize it).Another way to have different validation rules between forms is simply by creating a specific schema for a given form and then having autoForm(schema=yourSpecificSchema ... instead of autoForm(collection="Meteor.users". Do not forget to register an helper so your schema is accessible from your form. You can refer on the official documentation for more details: https://github.com/aldeed/meteor-autoform#non-collection-forms

这篇关于Meteor AutoForm 设置可选字段为必填项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-08 02:43