简体   繁体   中英

How to prevent false values from being added in meteor-autoform and SimpleSchema

I have a MongoDB collection with an attached SimpleSchema inside a Meteor application. On the frontend I'm using quickform from the meteor-autoform package to quickly render a form. ( 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);

Frontend form:

{{> quickForm collection="MyCollection" doc=myDoc id="updateMyDoc" type="update"}}

The quickform loads my exampleDoc with the isSuperDuper Boolean having an assigned value of false . I'm seeing it on the client in the console with AutoForm.getFormValues('updateMyDoc') If I check the mongo shell there is no isSuperDuper key, as expected.The second I save the quickForm (without making any changes) the doc is assigned isSuperDuper: false .

Is it possible to use quickform without it automatically converting empty Booleans from my schema into false values?

I have tried messing with the quickform settings autoConvert=false and removeEmptyStrings=false to no avail.

Thank you for your help.

I'm guessing the issue is you are using the default "checkbox" which only has two possible states (checked/unchecked). Unchecked would map to false .

See this section of the documentation: 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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM