簡體   English   中英

如何防止在meteor-autoform和SimpleSchema中添加錯誤值

[英]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=falseremoveEmptyStrings=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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM