更新时需要AutoForm 5.0.2嵌套模式输入 [英] AutoForm 5.0.2 nested schema inputs required on update

查看:67
本文介绍了更新时需要AutoForm 5.0.2嵌套模式输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经设置了模式,以便可以拥有一组复杂的输入集.像这样:

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.

这在4.2.2版中起作用(我相信是).这仍然适用于insert类型的自动套用格式,但不适用于update类型的自动套用格式.进行更新时,除非嵌套架构中的所有必填字段也有效,否则不会提交任何字段.

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}} 

我的模板(autoupdate)我复制粘贴了bootstrap3自动形成模板的全部内容,并重新排列了一些html以适合我的需要.我在更新时根据5.0.0更改日志将这些更新更新至最佳状态.如果有人可以想到模板中的某个属性,该属性会导致在5.0.0中更改的插入和更新之间出现不一致的行为,则可能在其中.

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.

更多信息

我刚刚尝试使用5.0.2版中的bootstrap3模板重新创建所有表单模板.仍然是相同的行为.

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

+

我在地址模式中有一个布尔值(复选框)输入.在文档中查看,地址数组中填充了[0 : {active_address: false}]

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...

+

根据@mark的建议,我添加了defaultValue:[].它解决了这个问题.现在,更新表单中没有开放"嵌套模式,可以更改其他值.如果使用添加"按钮将嵌套模式添加"到表单,那么即使您没有在任何字段中插入任​​何值,整个表单也将是必需的.不管Boolean类型输入如何,都会发生这种情况.

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.

我可以在嵌套模式中确定Boolean类型的输入,以使整个嵌套模式成为执行插入操作所必需的.删除布尔输入会导致它再次可插入.因此,同样存在一个新问题.

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.

可以在此处

推荐答案

我认为最好的解决方案是在架构的address字段中添加defaultValue: [].您在问题中描述的行为(不允许更新)实际上是故意的-请继续阅读以了解原因.

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.

问题是,仅当数组表单元素已添加到表单中时,此行为才存在.我的意思是,如果您单击从表单中删除街道,城市等输入的减号,则更新成功,因为当用户明确取消选中该复选框时,AutoForm不会误解未选中的复选框(并因此设置值到false).将defaultValue设置为空数组可以使AutoForm知道不显示地址表单,除非用户明确单击加号(即他们具有要输入的地址),在这种情况下,制作街道的行为,城市等.您需要的是必填字段.

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.

请注意,这意味着您必须更新集合中缺少address字段的现有文档,并将其设置为空数组.在mongo shell中是这样的:

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.

修改

如果您想要的行为是显示子表单而没有使其成为必需,则可以使用formToDoc钩子并过滤出仅包含以下内容的所有地址对象,以解决复选框问题active_address字段设置为false(AutoForm错误地为我们添加的字段).

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;
  }
});

formToDoc挂钩在每次验证表单时都会被调用,因此您可以使用它来修改文档以使其生效,从而使AutoForm永远不会知道有一个地址子字段,除非它具有属性已设置.请注意,如果您使用的是此解决方案,则不必如上所述添加defaultValue: [].

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.

这篇关于更新时需要AutoForm 5.0.2嵌套模式输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆