sgds-forms
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseSGDS Form Validation Pattern
SGDS 表单验证模式
SGDS form components integrate with the browser's ElementInternals API so they behave like native HTML form controls — they participate in submission, , and constraint validation automatically.
<form>FormDataPrerequisites
前置要求
See sgds-components for installation and framework integration.
All form components must be placed inside a element and given a attribute to participate in form submission.
<form>name安装和框架集成请参考**sgds-components**。
所有表单组件必须放置在元素内部,并设置属性,才能参与表单提交。
<form>nameQuick Decision Guide
快速决策指南
Show native HTML validation messages? → Add to each form component
hasFeedbackPrevent submit when fields are invalid? → Built-in — no extra code needed
Read submitted field values? → Use in the submit handler
new FormData(event.target)Disable SGDS validation per component? → prop on or (others WIP)
noValidate<sgds-input><sgds-textarea>Disable SGDS validation for the whole form? → on the element
novalidate<form>Run 3rd-party validation (e.g. Zod)? → Disable SGDS validation first, then call and set programmatically
setInvalid(bool)invalidFeedback是否显示原生HTML验证消息? → 为每个表单组件添加
hasFeedback字段无效时阻止提交? → 内置功能——无需额外代码
读取提交的字段值? → 在提交处理函数中使用
new FormData(event.target)按组件禁用SGDS验证? → 在或上添加属性(其他组件开发中)
<sgds-input><sgds-textarea>noValidate为整个表单禁用SGDS验证? → 在元素上添加属性
<form>novalidate使用第三方验证(如Zod)? → 先禁用SGDS验证,再通过编程方式调用并设置
setInvalid(bool)invalidFeedbackHow Validation Is Triggered
验证触发时机
SGDS validation is layered:
- onChange (after blur) — validates when the user leaves a field; shows feedback if is set
hasFeedback - onSubmit — final validation pass fires before submission; blocks the form if any field is invalid
- onReset — clears all validity states and field values when the form is reset
Disabled components skip validation entirely.
SGDS验证分为多层:
- onChange(失去焦点后) —— 用户离开字段时触发验证;若设置了则显示反馈
hasFeedback - onSubmit —— 提交前执行最终验证;若存在无效字段则阻止表单提交
- onReset —— 表单重置时清除所有有效性状态和字段值
禁用状态的组件会完全跳过验证。
hintText
and Error Message Placement
hintTexthintText
与错误消息的位置
hintTextOn most form components (, , , , , ), and the error message share the same space below the input container:
<sgds-input><sgds-textarea><sgds-select><sgds-combo-box><sgds-quantity-toggle><sgds-file-upload>hintText- When the field is invalid (and is set), the error message replaces
hasFeedback.hintText - When the error is resolved, reappears.
hintText
<sgds-checkbox-group><sgds-radio-group>hintText在大多数表单组件(、、、、、)中,和错误消息共享输入容器下方的同一空间:
<sgds-input><sgds-textarea><sgds-select><sgds-combo-box><sgds-quantity-toggle><sgds-file-upload>hintText- 当字段无效(且设置了)时,错误消息会替换
hasFeedback。hintText - 当错误解决后,会重新显示。
hintText
<sgds-checkbox-group><sgds-radio-group>hintTextActivating Feedback Display
激活反馈显示
Constraint validation runs regardless of , but the visible error message only renders when is present. The prop accepts:
hasFeedbackhasFeedbackhasFeedback| Value | Behaviour |
|---|---|
| Shows the native HTML validation message as error text |
| Same as boolean — shows text feedback only |
| Shows invalid border/colour styling only, no text |
| Shows both invalid styling and text feedback |
You can also override the displayed message with :
invalidFeedbackhtml
<sgds-input
name="email"
label="Email"
type="email"
required
hasFeedback="both"
invalidFeedback="Please enter a valid email address"
></sgds-input>无论是否设置,约束验证都会运行,但只有当存在时,才会渲染可见的错误消息。属性支持以下取值:
hasFeedbackhasFeedbackhasFeedback| 取值 | 行为 |
|---|---|
| 将原生HTML验证消息作为错误文本显示 |
| 与布尔值效果相同——仅显示文本反馈 |
| 仅显示无效边框/颜色样式,不显示文本 |
| 同时显示无效样式和文本反馈 |
你也可以通过覆盖显示的消息:
invalidFeedbackhtml
<sgds-input
name="email"
label="Email"
type="email"
required
hasFeedback="both"
invalidFeedback="Please enter a valid email address"
></sgds-input>Constraint Validations by Component
各组件支持的约束验证
| Component | Supported constraints |
|---|---|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| 组件 | 支持的约束规则 |
|---|---|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
Full Form Example
完整表单示例
html
<form id="my-form" class="d-flex-column">
<sgds-input
label="First Name"
name="firstName"
required
hasFeedback="both"
pattern="[A-Za-z ]+"
invalidFeedback="Letters only"
></sgds-input>
<sgds-datepicker
label="Appointment Date"
name="appointmentDate"
required
hasFeedback
></sgds-datepicker>
<sgds-radio-group label="Gender" name="gender" required hasFeedback>
<sgds-radio value="female">Female</sgds-radio>
<sgds-radio value="male">Male</sgds-radio>
</sgds-radio-group>
<sgds-checkbox-group
label="Food Preference"
name="food"
required
hasFeedback
hintText="Select at least one option"
>
<sgds-checkbox value="vegetarian">Vegetarian</sgds-checkbox>
<sgds-checkbox value="halal">Halal</sgds-checkbox>
</sgds-checkbox-group>
<sgds-textarea
label="Comments"
name="comments"
required
minlength="3"
hasFeedback
></sgds-textarea>
<sgds-button type="submit">Submit</sgds-button>
<sgds-button type="reset" variant="ghost">Reset</sgds-button>
</form>html
<form id="my-form" class="d-flex-column">
<sgds-input
label="First Name"
name="firstName"
required
hasFeedback="both"
pattern="[A-Za-z ]+"
invalidFeedback="Letters only"
></sgds-input>
<sgds-datepicker
label="Appointment Date"
name="appointmentDate"
required
hasFeedback
></sgds-datepicker>
<sgds-radio-group label="Gender" name="gender" required hasFeedback>
<sgds-radio value="female">Female</sgds-radio>
<sgds-radio value="male">Male</sgds-radio>
</sgds-radio-group>
<sgds-checkbox-group
label="Food Preference"
name="food"
required
hasFeedback
hintText="Select at least one option"
>
<sgds-checkbox value="vegetarian">Vegetarian</sgds-checkbox>
<sgds-checkbox value="halal">Halal</sgds-checkbox>
</sgds-checkbox-group>
<sgds-textarea
label="Comments"
name="comments"
required
minlength="3"
hasFeedback
></sgds-textarea>
<sgds-button type="submit">Submit</sgds-button>
<sgds-button type="reset" variant="ghost">Reset</sgds-button>
</form>Reading Form Values via FormData
通过FormData读取表单值
Use the native API in the submit handler. Access files separately via the component's property:
FormDatasgds-file-uploadselectedFileshtml
<script>
const form = document.getElementById("my-form");
form.addEventListener("submit", event => {
event.preventDefault();
const formData = new FormData(event.target);
const firstName = formData.get("firstName");
const gender = formData.get("gender");
const comments = formData.get("comments");
// File upload files are not in FormData automatically
const fileUpload = document.querySelector("sgds-file-upload");
for (let i = 0; i < fileUpload.selectedFiles.length; i++) {
formData.append("file" + i, fileUpload.selectedFiles[i]);
}
// Submit formData to server
});
</script>在提交处理函数中使用原生 API。需通过组件的属性单独访问上传的文件:
FormData<sgds-file-upload>selectedFileshtml
<script>
const form = document.getElementById("my-form");
form.addEventListener("submit", event => {
event.preventDefault();
const formData = new FormData(event.target);
const firstName = formData.get("firstName");
const gender = formData.get("gender");
const comments = formData.get("comments");
// 文件上传的文件不会自动加入FormData
const fileUpload = document.querySelector("sgds-file-upload");
for (let i = 0; i < fileUpload.selectedFiles.length; i++) {
formData.append("file" + i, fileUpload.selectedFiles[i]);
}
// 将formData提交至服务器
});
</script>Custom Validation (Disabling SGDS Validation)
自定义验证(禁用SGDS验证)
For 3rd-party validation libraries (e.g. Zod) or fully custom logic, disable SGDS's built-in validation first, then call and update in response to input events.
setInvalid(bool)invalidFeedback对于第三方验证库(如Zod)或完全自定义的逻辑,需先禁用SGDS的内置验证,再响应输入事件调用并更新。
setInvalid(bool)invalidFeedbackOption 1 — Disable per component with noValidate
noValidate选项1 —— 按组件使用noValidate
禁用
noValidateCurrently supported on and only. Other components are WIP.
<sgds-input><sgds-textarea>html
<sgds-input
noValidate
id="keys-input"
name="keys"
label="Keys"
hintText="Cannot start with special characters"
hasFeedback="both"
></sgds-input>
<script>
const input = document.getElementById("keys-input");
input.addEventListener("sgds-input", e => {
if (/^[^a-zA-Z0-9]/.test(e.target.value)) {
e.target.setInvalid(true);
e.target.invalidFeedback = "Keys cannot start with special characters";
} else {
e.target.setInvalid(false);
}
});
</script>目前仅支持**和**。其他组件开发中。
<sgds-input><sgds-textarea>html
<sgds-input
noValidate
id="keys-input"
name="keys"
label="Keys"
hintText="Cannot start with special characters"
hasFeedback="both"
></sgds-input>
<script>
const input = document.getElementById("keys-input");
input.addEventListener("sgds-input", e => {
if (/^[^a-zA-Z0-9]/.test(e.target.value)) {
e.target.setInvalid(true);
e.target.invalidFeedback = "Keys cannot start with special characters";
} else {
e.target.setInvalid(false);
}
});
</script>Option 2 — Disable at form level with novalidate
novalidate选项2 —— 在表单层级使用novalidate
禁用
novalidateAdding to the element disables constraint validation and SGDS validation for all child components. Then apply logic per field.
novalidate<form>setInvalidhtml
<form id="custom-form" novalidate class="d-flex-column">
<sgds-input
id="keys-input"
name="keys"
label="Keys"
hasFeedback="both"
></sgds-input>
<sgds-textarea
id="bio-textarea"
name="bio"
label="Bio"
hasFeedback
></sgds-textarea>
</form>
<script>
document.getElementById("keys-input").addEventListener("sgds-input", e => {
if (/^[^a-zA-Z0-9]/.test(e.target.value)) {
e.target.setInvalid(true);
e.target.invalidFeedback = "Invalid key format";
} else {
e.target.setInvalid(false);
}
});
document.getElementById("bio-textarea").addEventListener("sgds-input", e => {
if (e.target.value.length < 10) {
e.target.setInvalid(true);
e.target.invalidFeedback = "Bio must be at least 10 characters";
} else {
e.target.setInvalid(false);
}
});
</script>在元素上添加会禁用约束验证和SGDS验证,对所有子组件生效。然后为每个字段应用逻辑。
<form>novalidatesetInvalidhtml
<form id="custom-form" novalidate class="d-flex-column">
<sgds-input
id="keys-input"
name="keys"
label="Keys"
hasFeedback="both"
></sgds-input>
<sgds-textarea
id="bio-textarea"
name="bio"
label="Bio"
hasFeedback
></sgds-textarea>
</form>
<script>
document.getElementById("keys-input").addEventListener("sgds-input", e => {
if (/^[^a-zA-Z0-9]/.test(e.target.value)) {
e.target.setInvalid(true);
e.target.invalidFeedback = "Invalid key format";
} else {
e.target.setInvalid(false);
}
});
document.getElementById("bio-textarea").addEventListener("sgds-input", e => {
if (e.target.value.length < 10) {
e.target.setInvalid(true);
e.target.invalidFeedback = "Bio must be at least 10 characters";
} else {
e.target.setInvalid(false);
}
});
</script>setInvalid(bool)
Method
setInvalid(bool)setInvalid(bool)
方法
setInvalid(bool)| Parameter | Type | Description |
|---|---|---|
| | |
Pair with setting the property on the element to control the displayed message.
setInvalid(true)invalidFeedback| 参数 | 类型 | 描述 |
|---|---|---|
| | |
将与设置元素的属性配合使用,可控制显示的消息。
setInvalid(true)invalidFeedbackCustom Validation Support Status
自定义验证支持状态
| Component | Status |
|---|---|
| ✅ Implemented |
| ✅ Implemented |
| WIP |
| WIP |
| WIP |
| WIP |
| WIP |
| WIP |
| WIP |
| 组件 | 状态 |
|---|---|
| ✅ 已实现 |
| ✅ 已实现 |
| 开发中 |
| 开发中 |
| 开发中 |
| 开发中 |
| 开发中 |
| 开发中 |
| 开发中 |
For AI agents
给AI Agent的提示
- Always place SGDS form components inside a element with
<form>attributes for them to participate in form submission andname.FormData - must be present on a form component for the error message to visually appear — constraint validation alone does not show UI feedback.
hasFeedback - To show both the invalid border style and the error message text, use . A plain boolean
hasFeedback="both"shows the message text only.hasFeedback - Use to override the browser's native constraint validation message with a custom string.
invalidFeedback - Constraint validation and form submission blocking are built-in — do not add extra submit event listeners to replicate this behaviour.
- Use in the submit handler to read values. For file uploads, read
new FormData(event.target)directly from theselectedFileselement.<sgds-file-upload> - When using custom/3rd-party validation on or
<sgds-input>, add<sgds-textarea>on the component (ornoValidateon the form), then callnovalidateand setelement.setInvalid(true/false)inside theelement.invalidFeedbackevent listener.sgds-input - Only and
<sgds-input>fully support custom validation via<sgds-textarea>+noValidate. All other form components have this feature as WIP.setInvalid - The reset button () automatically clears all validity states and values — no extra reset logic is needed.
type="reset" - Disabled components are excluded from constraint validation and will never block form submission.
- 务必将SGDS表单组件放置在带有属性的
name元素内部,这样它们才能参与表单提交和<form>处理。FormData - 表单组件必须设置才会可视化显示错误消息——仅约束验证不会显示UI反馈。
hasFeedback - 若要同时显示无效边框样式和错误消息文本,请使用。纯布尔值
hasFeedback="both"仅显示消息文本。hasFeedback - 使用可将浏览器原生约束验证消息替换为自定义字符串。
invalidFeedback - 约束验证和表单提交阻止是内置功能——无需添加额外的提交事件监听器来复现此行为。
- 在提交处理函数中使用读取值。对于文件上传,直接从
new FormData(event.target)元素读取<sgds-file-upload>。selectedFiles - 在或
<sgds-input>上使用自定义/第三方验证时,需在组件上添加<sgds-textarea>(或在表单上添加noValidate),然后在novalidate事件监听器中调用sgds-input并设置element.setInvalid(true/false)。element.invalidFeedback - 只有和
<sgds-input>完全支持通过<sgds-textarea>+noValidate实现自定义验证。所有其他表单组件的此功能仍在开发中。setInvalid - 重置按钮()会自动清除所有有效性状态和值——无需额外的重置逻辑。
type="reset" - 禁用状态的组件会被排除在约束验证之外,永远不会阻止表单提交。