【问题标题】:How to dynamically change the value of an optional attribute of a field in Autoform / SimpleSchema?如何动态更改 Autoform / SimpleSchema 中字段的可选属性的值?
【发布时间】:2019-01-03 06:30:03
【问题描述】:

我有一个带有两个字段的 SimpleSchema 实例:

 isApproved: {
    type: Boolean,
    defaultValue: false
  },
  impactedSystems: {
    type: String,
    optional: true
  }

如果 isApproved 设置为 true,我想将 impactedSystemsoptional 属性更改为 false

我已尝试按照docs 中的说明进行操作,但无法正常工作。它建议我像这样向 impactedSystems 添加一个自定义函数(用我的字段名称修改):

custom: function () {
  var shouldBeRequired = this.field('isApproved').value == 1;

  if (shouldBeRequired) {
    // inserts
    if (!this.operator) {
      if (!this.isSet || this.value === null || this.value === "") return "required";
    }

    // updates
    else if (this.isSet) {
      if (this.operator === "$set" && this.value === null || this.value === "") return "required";
      if (this.operator === "$unset") return "required";
      if (this.operator === "$rename") return "required";
    }
  }
}

无论 isApproved 是否为真,该字段都是可选的。

我也尝试了来自this 答案的代码,但是当它所依赖的字段 (isApproved) 更新时,optional 的值不会改变。

如何让optional 的值与另一个布尔类型字段相反?!

【问题讨论】:

    标签: meteor meteor-autoform simple-schema


    【解决方案1】:

    我终于明白了。我从一个表单中插入 isApproved,然后在另一个表单中更新 impactedSystems。我所要做的就是将 isApproved (带有type=hidden)包含在我希望读取其值的更新表单中。我提供的代码是正确的。

    示例

    {#autoForm collection=Requests doc=this id="singleRequestLayout" type="update"}} //This can also be of type insert
        {{> afQuickField name="isApproved" type="hidden"}}
        {{> afQuickField name="impactedSystems"}}
    
        <button type="submit" class="button">Submit</button>
    {{/autoForm}}
    

    【讨论】:

      【解决方案2】:

      试试这个代码。诚然,这有点令人费解……

      这是一个通用助手,您可以在所有架构中使用:

       // This helper method does the ceremony around SimpleSchema's requirements
      
      export const isRequired = (thing,shouldBeRequired) => {
        if (shouldBeRequired) {
          // inserts
          if (!thing.operator) {
            if (!thing.isSet || thing.value === null || thing.value === "") return "required";
          }
      
          // updates
          else if (thing.isSet) {
            if (thing.operator === "$set" && thing.value === null || thing.value === "") return "required";
            if (thing.operator === "$unset") return "required";
            if (thing.operator === "$rename") return "required";
          }
        }
      };
      

      在架构本身中,您可以这样做:

      const isRequiredWhenApproved = (record) => {
        const shouldBeRequired = (record.field('isApproved').value);
        return isRequired(record, shouldBeRequired);
      };
      
        isApproved: {
          type: Boolean,
          defaultValue: false
        },
        impactedSystems: {
          type: String,
          optional: true,
          custom() {
            return isRequiredWhenApproved(this);
          },
        },
      

      希望对你有用

      【讨论】:

      • 谢谢!我提供的代码也被证明是正确的。我有两种不同形式的两个字段,导致一个(在每个表单上)未定义。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-01-08
      • 2021-11-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-26
      相关资源
      最近更新 更多