嵌套模式中的布尔值导致所需状态 [英] Boolean in nested schema causing required state

查看:40
本文介绍了嵌套模式中的布尔值导致所需状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我设置了 schemas 以便我可以拥有一组复杂的输入通过 autoform 设置.类似的东西:

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.

尝试提交表单会为地址"下的每个字段抛出一个必填错误,除了 Boolean,即使未选中复选框也是如此.

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

我正在大量使用基于自动表单随附的 bootstrap3 表单模板的自定义表单模板.

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

尝试过

尝试像这样添加一个钩子:

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.

推荐答案

我在上次评估中错了.我已经从人员架构的地址字段中删除了 defaultValue: [].将其与 formToDoc 挂钩中的以下代码一起使用可解决此问题:

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;

这篇关于嵌套模式中的布尔值导致所需状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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