首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何防止在meteor-autoform和SimpleSchema中添加假的值

如何防止在meteor-autoform和SimpleSchema中添加假的值
EN

Stack Overflow用户
提问于 2020-06-07 05:31:03
回答 1查看 48关注 0票数 0

我有一个在Meteor应用程序中附加了SimpleSchema的MongoDB集合。在前端,我使用meteor-autoform包中的quickform来快速呈现表单。(https://github.com/Meteor-Community-Packages/meteor-autoform)

代码语言:javascript
复制
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);

前端表单:

代码语言:javascript
复制
{{> quickForm collection="MyCollection" doc=myDoc id="updateMyDoc" type="update"}}

quickform使用isSuperDuper布尔值加载我的exampleDoc,该布尔值的赋值为false。我在使用AutoForm.getFormValues('updateMyDoc')的客户端控制台上看到它,如果我检查mongo shell没有isSuperDuper密钥,正如预期的那样。当我保存quickForm时(没有做任何修改),文档就被分配了isSuperDuper: false

在不自动将我的模式中的空布尔值转换为false值的情况下,是否可以使用quickform

我曾尝试修改quickform设置autoConvert=falseremoveEmptyStrings=false,但都无济于事。

谢谢你的帮助。

EN

回答 1

Stack Overflow用户

发布于 2020-06-07 07:52:51

我猜问题是你使用了默认的"checkbox“,它只有两种可能的状态(选中/未选中)。未选中将映射到false

请参阅文档的这一部分:https://github.com/Meteor-Community-Packages/meteor-autoform#affieldinput

代码语言:javascript
复制
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.

这听起来像是您可以指定单选或选择选项来允许null状态,这可能会为可选的布尔值产生更好的结果(本质上是给它第三个状态)。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62238262

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档