1
votes

I have schemas set up so that I can have an array of complex input sets. 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.

This worked in (I believe it was) version 4.2.2. This still works on insert type autoforms, but not on update type autoforms. Doing an update, none of the fields will submit unless all required fields in the nested schema are also valid.

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

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

My templates (autoupdate) I copy-pasted the entirety of bootstrap3 autoform templates and rearranged some of the html to fit my needs. I updated these to the best of my ability according to the 5.0.0 changelog when I updated. It could possibly be in there if someone can think of an attribute in the templates that would cause inconsistent behavior between insert and update that changed in 5.0.0.

More information

I just tried recreating all of my form templates using the bootstrap3 templates from 5.0.2. Still the same behavior.

+

I have a Boolean (checkbox) input in the address schema. Looking in a doc, the address array is populated with [0 : {active_address: false}]

active_address: {
  type: Boolean,
  optional: true
}

Not sure if that helps...

+

As per @mark's suggestion, I added defaultValue:[]. It fixed the issue... sort of. There are no "open" nested schemas in the update form now, and other values can be changed. If you "add" a nested schema to the form with the add button, that entire form becomes required even if you don't insert any value in any field. This happens regardless of the Boolean type input.

I can nail down the Boolean type input in the nested schema causes that entire nested schema to become necessary to do the insert. Removing the Boolean input caused it to be insertable again. So there's a new problem in the same vein.

This new issue can be found here

1
What does the document that you are updating look like? Do you have any autoValue/defaultValue fields in the address field by any chance?mark
@mark I do not. Are you looking for an actual example doc or the schema?Randy Hall
@mark Got me thinking, so I checked out my doc and added some info.Randy Hall
Right, so I think the active address input is the culprit. Its causing autoform to add a doc that is missing all the required fields like street and city, which naturally fails schema validation. I havent personally used the boolean input before but I suspect that autoform mistakes the unchecked box as "false" rather than undefined. Ill have to check whether this is the case when I get to a computer.mark
@mark I'll set up a test overriding the output of that field as undefined instead of false... or maybe removing it from the $set object? We'll seeRandy Hall

1 Answers

1
votes

I think the best solution is to add a defaultValue: [] to the address field in the schema. The behavior you described in the question (not allowing the update) is actually intended -- read on to see why.

The thing is, this behavior only exists if an array form element has already been added to the form. What I mean is, if you click the minus sign that removes the street, city, etc. inputs from the form, the update succeeds because AutoForm doesn't misinterpret the unchecked checkbox as the user explicitly unchecking the box (and therefore setting the value to false). Setting the defaultValue to an empty array lets AutoForm know to not present the address form unless the user has explicitly clicked the plus sign (i.e, they have an address they want to enter), in which case the behavior of making the street, city, etc. fields required is what you want.

Note that this means you'll have to update the existing documents in your collection that are missing the address field and set it to an empty array. Something like this in the mongo shell:

db.people.update({ "address": { $exists: false } }, { $set: { "address": [] } }, { multi: true })

You'll probably want to make sure that the query is correct by running a find on the selector first.

Edit

If the behavior you want is to show the sub-form without making it required, you can work around the checkbox issue by using the formToDoc hook and filtering out all address objects that only have the active_address field set to false (the field that AutoForm mistakenly adds for us).

AutoForm.addHooks('yourFormId', {
  formToDoc: function (doc) {
    doc.address = _.reject(doc.address, function (a) {
      return !a.street && !a.city && !a.active_address;
    });
    return doc;
  }
});

The formToDoc hook is called every time the form is validated, so you can use it to modify the doc to make it so that AutoForm is never even aware that there is an address sub-field, unless a property of it has been set. Note that if you're using this solution you won't have to add the defaultValue: [] as stated above.