如何使用羊驼毛创建所需的条件字段? [英] How to create a required conditional field using Alpaca?

查看:73
本文介绍了如何使用羊驼毛创建所需的条件字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Does anyone know how I can define a required field which is dependant on another field?

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

For example if field1 is marked true then field2 must be required, otherwise field 2 should not be filled.

这是我目前的尝试:

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

推荐答案

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

Alpaca's dependency system hides the dependant field if the dependency is not met, otherwise the field is shown and any options assigned to it such as validation options are also required.

浏览文档后,我注意到您必须在模式和选项对象中都设置依赖项.

After looking through the documentation I noticed that you have to set the dependencies in both the schema and the options objects.

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
        }
      }
    }
  }
}

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

In the above example, we have a simple select with the options of Y and N. If Y is selected then we show a required textarea, otherwise the textarea is not displayed.

实时示例

JSFiddle -注意表单对象中的注释.

JSFiddle - Take note of the comments in the form object.

这篇关于如何使用羊驼毛创建所需的条件字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆