如何使用羊驼创建一个必需的条件字段?

Mig*_*rgo 6 javascript forms alpacajs

有谁知道如何定义一个依赖于另一个字段的必填字段?

例如,如果field1标记,true那么field2必须是必需的,否则不应填写字段2.

这是我目前的尝试:

"field1": {
    "title": "Field1:",
    "type": "string",
    "enum": ["true", "false"]
},
"field2": {
    "title": "Field2:",
    "type": "integer",
    "dependencies": "field1",
    "required": true
}
Run Code Online (Sandbox Code Playgroud)

Scr*_*t47 3

如果不满足依赖关系,Alpaca 的依赖系统会隐藏依赖字段,否则会显示该字段,并且还需要分配给它的任何选项(例如验证选项)。

查看文档后,我注意到您必须在架构和选项对象中设置依赖项。

JSON

{
  "view": "bootstrap-edit",
  "schema": {
    "type": "object",
    "properties": {
      "description_required": {
        "enum": [
          "Y",
          "N"
        ],
        "required": true
      },
      "description": {
        "required": true
      }
    },
    "dependencies": {
      "description": ["description_required"] // Specify the field that your conditional field is dependant on
    }
  },
  "options": {
    "fields": {
      "description_required": {
        "type": "select",
        "noneLabel": "Select an Option",
        "label": "Description Required"
      },
      "description": {
        "type": "textarea",
        "cols": 5,
        "label": "Description",
        "dependencies": {
          "description_required": "Y" // Specify the required value for the dependant field to show
        }
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

在上面的示例中,我们有一个带有Y和N选项的简单选择。如果选择Y ,则我们显示所需的文本区域,否则不显示文本区域。

实例

JSFiddle - 记下表单对象中的注释。