簡體   English   中英

Angular 模式表單 - 如果 Integer 字段不是 Null,則需要字段

[英]Angular Schema Form - Require Field if Integer Field is Not Null

我正在嘗試使用 Angular JSON 模式表單創建一個表單。 我希望在填充另一個字段(number1)時需要一個字段(dropdown1)。 我能夠在SchemaForm.io Sandbox中獲得以下表單 + 架構,但是當我將其放入我的站點時,下拉字段丟失。

這是我的架構:

{
"type": "object",
"properties": {
    "isChecked": {
        "title": "checked?",
        "type": "boolean"
    },
    "text1": {
        "title": "text1",
        "type": "number",
        "minimum": 0,
        "maximum": 100,
        "condition": "model.isChecked"
    },
    "number1": {
        "title": "number1",
        "type": "number",
        "minimum": 0,
        "condition": "model.isChecked"
    },
    "dropdown1": {
        "title": "",
        "type": "string",
        "enum": ["Days", "Months", "Years"],
        "condition": "model.isChecked"
    },
    "comment": {"type": "string", "Title": "Comment"}
}

}

這是我的表格:

[
"isChecked",
{
    "key": "text1",
    "type": "decimal",
    "validationMessages": {
        "maximum": "text1 must be 100 or lower.",
        "minimum": "text1 must be 0 or higher.",
        "required": "This field is required"
    },
    "required": false
},
{
    "key": "number1",
    "type": "number",
    "validationMessages": {
        "minimum": "number1 must be 0 or higher."
    },
    "required": false
},
{
    "key": "dropdown1",
    "required": false,
    "condition": "model.number1 === null"
},
{
    "key": "dropdown1",
    "required": true,
    "condition": "model.number1 > 0",
    "validationMessages": {
        "required": "dropdown1 is required."
    }
},
{
    "key": "comment",
    "type": "textarea"
}

]

您可以使用“依賴項”屬性( https://json-schema.org/understanding-json-schema/reference/object.html#property-dependencies )。 該示例要求在提供 credit_card 時提供 billing_address:

{
  "type": "object",
  "properties": {
    "credit_card": {
      "type": "number"
    },
    "billing_address": {
      "type": "string"
    }
  },
  "dependencies": {
    "credit_card": [
      "billing_address"
    ]
  }
}

此實現支持“依賴項”: https://github.com/dashjoin/json-schema-form

您可以在此處查看在線示例: https://dashjoin.github.io/#/example/dependencies

這是我找到的解決方案:

"condition": {
                "functionBody": "return model.number1 === undefined && model.isChecked"
            }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM