javascript - 如何使用羊驼创建必填条件字段?

标签 javascript forms alpacajs

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

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

这是我目前的尝试:

"field1": {
    "title": "Field1:",
    "type": "string",
    "enum": ["true", "false"]
},
"field2": {
    "title": "Field2:",
    "type": "integer",
    "dependencies": "field1",
    "required": true
}

最佳答案

如果不满足依赖关系,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
        }
      }
    }
  }
}

在上面的例子中,我们有一个简单的选择选项YN。如果选择了 Y,那么我们会显示一个必需的文本区域,否则不会显示该文本区域。

实例

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

关于javascript - 如何使用羊驼创建必填条件字段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46124024/

相关文章:

javascript - 在每个系列 Highcharts 中添加 Xaxis 数据

swift - Eureka : cannot convert value of type 'BaseRow?' to specified type 'CustomPushRow?'

django - 只读表单字段格式

api - Julia API 调用 Alpaca 交易收到 HTTP/1.1 401 Unauthorized

python - 从外部源访问羊驼表单中的字段

javascript - 如何在 vue 3 和 typescript 中正确键入 vuex 模块

javascript - Chrome 无法读取 style.display 值

php - PHP表单未更新sqlite数据库

javascript - 如何将 input-group-addon ( Bootstrap CSS ) 添加到 Alpaca.js 字段

JavaScript 表单验证仍然在 'False' 上提交表单