表單檢視(uiFormDefinition)概述

表單檢視是 Azure 入口網站創建體驗的 JSON 描述。 Azure 入口網站會在執行時從 JSON 渲染表單;不需要寫客戶端程式碼。 表單檢視使用 uiFormDefinition.json 格式,並由 模板規範入口表單使用。 Azure 受控應用程式使用獨立的createUiDefinition.json 格式。

最小檔案長這樣:

{
  "$schema": "https://schema.management.azure.com/schemas/2021-09-09/uiFormDefinition.schema.json#",
  "view": {
    "kind": "Form",
    "properties": {
      "title": "Create my resource",
      "steps": []
    },
    "outputs": {
      "kind": "ResourceGroup",
      "resourceGroupId": "",
      "location": "",
      "parameters": {}
    }
  }
}

表單視圖總是有:

Field Required Description
$schema Recommended 為 IntelliSense 釘選2021-09-09/uiFormDefinition.schema.json結構描述。
view.kind Yes 必須是 "Form"
view.properties.title Yes 頁面標題顯示在表格上方。
view.properties.steps Yes 一個或多個步驟(制表符)。 請參閱 階梯
view.outputs 是的(部署時) 部署內容——即部署的目標為哪個訂閱、資源群組、管理群組或租用戶,以及 Azure Resource Manager 範本的參數值。 請參見 輸出

Steps

steps 是一個有序陣列。 每個步驟都會以 tab 格式呈現。使用者在 下一步上一階段之間切換。

"steps": [
  {
    "name": "basics",
    "label": "Basics",
    "description": "Provide the basic settings for the resource.",
    "elements": [
      {
        "name": "name",
        "type": "Microsoft.Common.TextBox",
        "label": "Resource name",
        "toolTip": "Provide a unique name.",
        "constraints": {
          "required": true,
          "regex": "^[a-z0-9]{3,24}$",
          "validationMessage": "3-24 lowercase letters or digits."
        }
      }
    ]
  }
]

每個步驟包含:

Field Required Description
name Yes 步驟識別碼。 從運算式中參考它作為steps('<name>')
label Yes 索引標籤圖格。
description No 可選描述,置於分頁標題下方。
elements Yes 一組表單控制項。 請參見 表單檢視元素

使用 Microsoft.Common.Section 將相關欄位分組,而不是讓單一步驟承載過多內容。 區段不可巢狀。

輸出(部署環境)

當表單檢視用於部署 Azure Resource Manager 範本時(常見情況),view.outputs就是部署上下文。 入口網站會用它來決定要 部署在哪裡 ,以及要傳 範本的參數值。 支援四個 kind 數值:

kind 必填欄位 使用時機
ResourceGroup resourceGroupIdlocationparameters 大部分資源 部署至資源群組。
Subscription subscriptionIdlocationparameters 模板的 $schemasubscriptionDeploymentTemplate.json
ManagementGroup managementGroupIdlocationparameters 模板的 $schemamanagementGroupDeploymentTemplate.json
Tenant locationparameters 模板的 $schematenantDeploymentTemplate.json

parameters金鑰對應 1:1 對應到 ARM 範本參數。 數值通常是讀取步進輸出的表達式。

對於資源群組部署,公開架構支援 Microsoft.Common.ResourceScope,而 「建立範本規範門戶表單 」教學則使用它來處理常見的訂閱、資源群組及位置選擇流程。 以下範例假設該 basics 步驟包含一個 Microsoft.Common.ResourceScope 名為 resourceScope的元素。 當表單包含控制項(例如 Microsoft.Compute.SizeSelector),可從部署範圍推斷訂閱與位置時,這是最安全的預設。

"outputs": {
  "kind": "ResourceGroup",
  "resourceGroupId": "[steps('basics').resourceScope.resourceGroup.id]",
  "location":        "[steps('basics').resourceScope.location.name]",
  "parameters": {
    "keyVaultName": "[steps('basics').name]",
    "location":     "[steps('basics').resourceScope.location.name]",
    "sku":          "[steps('keyvault').sku]"
  }
}

若表單使用獨立的 Microsoft.Common.SubscriptionSelectorMicrosoft.Common.ResourceGroupSelectorMicrosoft.Common.LocationSelector 元素,而非 Microsoft.Common.ResourceScope,請確保具範圍感知功能的控制項會透過其 scope 屬性,明確繫結至所選的訂閱和位置。

表達方式

形式的字串 "[ ... ]" 被當作表達式來評估。 這些函式與 CreateUiDefinition 函式中所記載的是同一組:

功能 Returns
steps('<stepName>') 物件會保留所有命名步驟中的控制輸出。
basics('<elementName>') 舊版 Basics 步驟中元素的輸出(僅適用於 CreateUiDefinition)。
equalsnotandorifcoalesceempty 合乎邏輯的幫手。
concatsplitsubstringindexOftoLowertoUpperlastfirst 字串與陣列輔助工具。
lengthminmaxaddsubmuldiv 數字輔助者。
subscription()resourceGroup()location() 已選取的範圍。

在控制項中, visibledefaultValue大多數 constraints.* 欄位接受字面值或表達式。

本地化

面向使用者的字串可以內嵌為字面值(如上述範例)。

創作工具

  • 表單檢視沙盒可以從 ARM 範本產生預設表單並預覽結果。 請參閱為範本規格建立入口網站表單,以取得端對端的逐步解說。

  • 該架構發表於 https://schema.management.azure.com/schemas/2021-09-09/uiFormDefinition.schema.json。 要在 Visual Studio Code 啟用 IntelliSense,請在表單檔中將結構網址作為頂層$schema值:

    {
      "$schema": "https://schema.management.azure.com/schemas/2021-09-09/uiFormDefinition.schema.json#",
      "view": {
        "kind": "Form"
      }
    }
    

    如果 Visual Studio Code 提示你信任該結構域,請選擇「配置受信任域」並新增https://schema.management.azure.com。 你也可以在指令面板的 偏好設定中加入:設定受信任網域

下一步