1

如何根据从另一个字段中选择的值有条件地要求或取消要求一个字段?

目标:

  • 如果选择的国家/地区是葡萄牙或越南,则需要“输入代码”字段
  • 否则,不需要“输入代码”字段

这是一个嵌套模式形式:

const schema = {
  "type": "object",
  "properties": {
    "myGroup": {
      "type": "object",
      "title": "My Group",
      "properties": {
        "country": {
          "type": "string",
          "title": "Country",
          "enum": ["", "RO", "PT", "JP", "VN"],
          "enumName": ["", "Romania", "Portugal", "Japan", "Vietnam"]
        },
        "code": {
          "type": "string",
          "title": "Input Code"
        }
      },
      "required": ["country"],
      "dependencies": {
        "country": {
          "oneOf": [
            {
              "not": {
                "properties": {
                  "country": { "enum": ["PT", "VN"] }
                }
              }
            },
            {
              "properties": {
                "country": { "enum": ["PT", "VN"] }
              },
              "required": ["code"]
            }
          ]
        }
      }
     }
   },
 };

这是一个工作代码,但我收到 2 个额外的验证错误 https://codesandbox.io/s/react-json-schema-require-field-based-on-another-field-selection-bbgrh?file=/src /index.js

  • .myGroup 不应该是有效的
  • .myGroup 应该与 oneOf 中的一个模式完全匹配

在此处输入图像描述

4

0 回答 0