Boolean in nested schema causing required state

2019-06-09 05:10发布

I have schemas set up so that I can have an array of complex input sets via autoform. Something like:

address = {
  street:{
    type: String
  },
  city: {
    type: String
  },
  active_address: {
    type: Boolean,
    optional: true
  },
  ...
}

people: {
  name:{
    type: String
  },

  address:{
    type: [address],
    optional: true,
    defaultValue: []
  }
}

This way adding an address is optional, but if you add an address all of the address fields are required.

Trying to submit the form throws a required error for every field under "address" except for the Boolean, even though the checkbox is not checked.

For reference, I'm creating the form as such:

{{#autoForm collection="people" id=formId type="insert" doc=getDocument autosave=true template="autoupdate"}}    
  {{> afQuickField name='name' template="autoupdate" placeholder="schemaLabel"}}
  {{> afQuickField name='address' template="autoupdate"}}
  ...
{{/autoForm}} 

I'm using custom form templates very heavily based on bootstrap3 form templates that come with autoform.

Tried

Tried adding a hook like so:

 formToDoc:function(doc, ss, formId){
    for (var i = 0, l = doc.address.length; i < l; ++i){
      if (!doc.address[i].active_address){
        delete doc.address[i].active_address;
      };
    }
    return doc;
  }

Which solves the submit problem, but still inserts an array full of empty strings "" for the other values. This causes the update form to go haywire, similar as to what's illustrated in my other question.

The issue is that the array isn't empty, but instead has an object of empty values. I could probably run over every value in the form and remove all the fields but that feels very hacky and expensive.

1条回答
爱情/是我丢掉的垃圾
2楼-- · 2019-06-09 05:16

I was incorrect in my last assessment. I had removed the defaultValue: [] from the address field in the person schema. Using that with the following code in the formToDoc hook fixes the issue:

for (var i = 0, l = doc.address.length; i < l; ++i){
  if (!doc.address[i].active_address){
    doc.address[i].active_address = null;
  }        
}
return doc;
查看更多
登录 后发表回答