我有以下(简化的) 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中使用了这个模式,所以我将这个字段设置为可选。但在上面提到的表单中,此字段也是必需的。那么,如何覆盖特定表单中字段的可选设置呢?
我已经尝试了以下方法,但没有成功(required不会在HTML中呈现):
{{> afFieldInput name="kicker2" type="select" options=this required="required"}}提前感谢!
发布于 2015-08-25 01:30:59
你有一些技巧可以根据情况设置可选值,一个很好的方法是在函数返回时设置可选值,如下所示:
EventSchema = new SimpleSchema({
eventType: {
type: String
},
kicker: {
type: String
},
kicker2: {
type: String,
optional: function() {
return (! this.isInsert)
}
}
});所以它在update时是可选的,但在insert时不是(您可以使用任何方式对其进行自定义)。在表单之间使用不同验证规则的另一种方法是为给定表单创建特定的模式,然后使用autoForm(schema=yourSpecificSchema ...代替autoForm(collection="Meteor.users"。不要忘记注册一个帮助器,以便可以从表单访问您的模式。有关更多详细信息,请参阅官方文档:https://github.com/aldeed/meteor-autoform#non-collection-forms
https://stackoverflow.com/questions/32112599
复制相似问题