[英]How to prevent false values from being added in meteor-autoform and SimpleSchema
我有一个 MongoDB 集合,在 Meteor 应用程序中附加了 SimpleSchema。 在前端,我使用来自meteor-autoform
quickform
package 的 quickform 来快速呈现表单。 ( https://github.com/Meteor-Community-Packages/meteor-autoform )
export const MyCollection = new Mongo.Collection('my-collection');
export const MySchema = new SimpleSchema({
name: {
type: String
},
isSuperDuper: {
type: Boolean,
optional: true
}
});
MyCollection.attachSchema(MySchema);
const exampleDoc = { name: 'Waka Waka' };
MyCollection.insert(exampleDoc);
前端形式:
{{> quickForm collection="MyCollection" doc=myDoc id="updateMyDoc" type="update"}}
快速表单使用quickform
加载我的 exampleDoc,其分配值为false
。 我在控制台的客户端上使用AutoForm.getFormValues('updateMyDoc')
isSuperDuper
它第二次我保存了 quickForm(不做任何更改),文档被分配为isSuperDuper: false
。
是否可以使用quickform
而不自动将我的架构中的空布尔值转换为false
值?
我试过弄乱快速表单设置autoConvert=false
和removeEmptyStrings=false
无济于事。
谢谢您的帮助。
我猜问题是您使用的是默认的“复选框”,它只有两种可能的状态(选中/未选中)。 未选中会将 map 设置为false
。
请参阅文档的这一部分: https://github.com/Meteor-Community-Packages/meteor-autoform#affieldinput
If you don't include a type attribute, the following logic is used to automatically select an appropriate type:
...
* Otherwise if the schema type is Boolean, the boolean-checkbox
type is used. You may want to specify a type of boolean-radios
or boolean-select instead. If you do so, use the trueLabel,
falseLabel, and nullLabel attributes to set the labels used
in the radio or select control.
This sounds like you can specify radio or select options to allow for a null state which may produce better results for an optional boolean (essentially giving it a third state).
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.