最適化Forms區塊欄位屬性

本檔案概述JSON結構描述如何對應至blocks/form/form.js中轉譯的HTML,著重於如何識別和轉譯欄位、常見模式以及欄位特有的差異。

如何識別欄位(fieldType)?

JSON結構描述中的每個欄位都有fieldType屬性可決定其呈現方式。 fieldType可以是:

  • 特殊型別
    範例: drop-downradio-groupcheckbox-grouppanelplain-textimageheading等。
  • 有效的HTML輸入型別
    範例: textnumberemaildatepasswordtelrangefile等。
  • 具有-input尾碼的型別
    範例: text-inputnumber-input等。 (已標準化為基底型別,例如textnumber)。

如果fieldType符合特殊型別,則會使用​ 自訂轉譯器。 否則,會將其視為​ 預設輸入型別

請參閱下列各節,瞭解每種欄位型別的完整HTML結構和屬性

欄位使用的通用屬性

以下是大部分欄位使用的屬性:

  • id:指定專案ID並支援協助工具。
  • name:定義輸入、選取或fieldset專案的name屬性。
  • label.valuelabel.richTextlabel.visible:指定標籤/圖例文字、HTML內容和可見度。
  • value:代表欄位目前的值。
  • required:新增required屬性或驗證資料。
  • readOnlyenabled:控制欄位是否可編輯或停用。
  • description:在欄位下方顯示說明文字。
  • tooltip:設定輸入的title屬性。
  • constraintMessages:提供自訂錯誤訊息作為資料屬性。

通用HTML結構

大部分欄位都會在包含標籤和選用說明文字的包裝函式中轉譯。 下列程式碼片段會示範結構:

<div class="<fieldType>-wrapper field-wrapper field-<name>" data-id="<id>">
<label for="<id>" class="field-label">Label</label>
<!-- Field-specific input/element here -->
<div class="field-description" id="<id>-description">Description or error
message</div>
</div>

對於群組(無線電/核取方塊)和面板,會使用具有<fieldset><legend>,而非<div>/<label>。 說明文字

只有在已設定說明時才會出現。

錯誤訊息顯示

錯誤訊息會顯示在用於說明文字(動態更新)的相同.field-description元素中。

當欄位無效時

欄位生效時

可使用JSON中的constraintMessages屬性來定義自訂錯誤訊息。
這些在包裝函式上新增為data-<constraint>ErrorMessage屬性(例如,data-requiredErrorMessage)。

預設輸入型別(HTML輸入或*-input)

如果fieldType不是特殊型別,則會將其視為標準HTML輸入型別或視為<type>-input,例如textnumberemaildatetext-inputnumber-input

預設輸入型別的限制

會根據JSON屬性,將限制新增為輸入元素上的屬性。

JSON屬性
HTML屬性
套用至
maxLength
maxlength
文字,電子郵件,密碼,電話
minlength
minlength
文字,電子郵件,密碼,電話
圖樣
圖樣
文字,電子郵件,密碼,電話
最大
最大值
數字、範圍、日期
最小值
分鐘
數字、範圍、日期
步驟
步驟
數字、範圍、日期
接受
接受
檔案
多個
多個
檔案
maxOccure
data-max
面板
minOccure
data-min
面板
NOTE
multiple是布林值屬性。 如果為true,則會新增multiple屬性。

這些屬性會由表單轉譯器根據欄位的JSON定義自動設定。

範例:含有限制的HTML結構

下列範例示範如何使用驗證限制和錯誤處理屬性來呈現數字欄位。

<div class="number-wrapper field-wrapper field-age" data-id="age"
data-required="true"
data-minimumErrorMessage="Too small" data-maximumErrorMessage="Too large">
<label for="age" class="field-label">Age</label>
<input type="number"
id="age" name="age"
value="30" required min="18"
max="99" step="1"
placeholder="Enter your age" />
<div class="field-description" id="age-description"> Description or error message
</div>
</div>

HTML結構的每個部分在資料繫結、驗證和顯示說明或錯誤訊息方面都扮演一個角色。

欄位特定屬性及其HTML結構

下拉式清單

額外屬性:

範例:

<div class="drop-down-wrapper field-wrapper field-<name>" data-id="<id>"
data-required="true"
data-requiredErrorMessage="This field is required">
<label for="<id>" class="field-label">Label</label>
<select id="<id>" name="<name>" required title="Tooltip" multiple>
<option disabled selected value="">Placeholder</option>
<option value="opt1">Option 1</option>
<option value="opt2">Option 2</option>
</select>
<div class="field-description" id="<id>-description"> Description or error message
</div>
</div>

純文字

額外屬性

範例:

<div class="plain-text-wrapper field-wrapper field-<name>" data-id="<id>">
<label for="<id>" class="field-label">Label</label>
<p>Text or <a href="..." target="_blank">link</a></p>
</div>

核取方塊

額外屬性

範例:

<div class="checkbox-wrapper field-wrapper field-<name>" data-id="<id>"
data-required="true"
data-requiredErrorMessage="Please check this box">
<label for="<id>" class="field-label">Label</label>
<input type="checkbox"
id="<id>"
name="<name>" value="on"
required
data-unchecked-value="off" />
<div class="field-description" id="<id>-description"> Description or error message
</div>
</div>

按鈕

額外屬性

範例:

<div class="button-wrapper field-wrapper field-<name>" data-id="<id>">
<button id="<id>" name="<name>" type="submit" class="button"> Label
</button>
</div>

多行輸入

額外屬性

範例:

<div class="multiline-wrapper field-wrapper field-<name>" data-id="<id>"
data-minLengthErrorMessage="Too short" data-maxLengthErrorMessage="Too long">
<label for="<id>" class="field-label">Label</label>
<textarea id="<id>"
name="<name>" required
minlength="2"
maxlength="100"
pattern="[A-Za-z]+"
placeholder="Type here..."></textarea>
<div class="field-description" id="<id>-description"> Description or error message
</div>
</div>

面板

額外屬性

範例:

<fieldset class="panel-wrapper field-wrapper field-<name>" data-id="<id>"
name="<name>"
data-repeatable="true" data-index="0">
<legend class="field-label">Label</legend>
<!-- Nested fields here -->
<button type="button" class="add">Add</button>
<button type="button" class="remove">Remove</button>
<div class="field-description" id="<id>-description"> Description or error message
</div>
</fieldset>

選項

額外屬性

範例:

<div class="radio-wrapper field-wrapper field-<name>" data-id="<id>"
data-required="true">
<label for="<id>" class="field-label">Label</label>
<input type="radio" id="<id>" name="<name>" value="opt1" required />
<div class="field-description" id="<id>-description"> Description or error message
</div>
</div>

單選按鈕群組

額外屬性

範例:

<fieldset class="radio-group-wrapper field-wrapper field-<name>" data-id="<id>"
data-required="true">
<legend class="field-label">Label</legend>
<div>
<input type="radio" id="<id>-0" name="<name>" value="opt1" required />
<label for="<id>-0">Option 1</label>
</div>
<div>
<input type="radio" id="<id>-1" name="<name>" value="opt2" />
<label for="<id>-1">Option 2</label>
</div>
<div class="field-description" id="<id>-description"> Description or error message
</div>
</fieldset>

核取方塊群組

額外屬性

範例:

<fieldset class="checkbox-group-wrapper field-wrapper field-<name>" data-id="<id>"
data-required="true" data-minItems="1"
data-maxItems="3">
<legend class="field-label">Label</legend>
<div>
<input type="checkbox" id="<id>-0" name="<name>" value="opt1" required />
<label for="<id>-0">Option 1</label>
</div>
<div>
<input type="checkbox" id="<id>-1" name="<name>" value="opt2" />
<label for="<id>-1">Option 2</label>
</div>
<div class="field-description" id="<id>-description"> Description or error message
</div>
</fieldset>

影像

額外屬性

範例:

<div class="image-wrapper field-wrapper field-<name>" data-id="<id>">
<picture>
<img src="..." alt="..." />
<!-- Optimized sources -->
</picture>
</div>

標題

額外屬性

範例:

<div class="heading-wrapper field-wrapper field-<name>" data-id="<id>">
<h2 id="<id>">Heading Text</h2>
</div>

如需詳細資訊,請參閱blocks/form/form.jsblocks/form/util.js中的實作。

recommendation-more-help
fbcff2a9-b6fe-4574-b04a-21e75df764ab