Loading...
Loading...
Use this skill when users ask about form validation in SGDS, hasFeedback prop, constraint validation, custom validation, noValidate, setInvalid, form submission, or reading FormData from SGDS form components.
npx skill4agent add govtechsg/sgds-web-component sgds-forms<form>FormData<form>namehasFeedbacknew FormData(event.target)noValidate<sgds-input><sgds-textarea>novalidate<form>setInvalid(bool)invalidFeedbackhasFeedbackhintText<sgds-input><sgds-textarea><sgds-select><sgds-combo-box><sgds-quantity-toggle><sgds-file-upload>hintTexthasFeedbackhintTexthintText<sgds-checkbox-group><sgds-radio-group>hintTexthasFeedbackhasFeedbackhasFeedback| 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 |
invalidFeedback<sgds-input
name="email"
label="Email"
type="email"
required
hasFeedback="both"
invalidFeedback="Please enter a valid email address"
></sgds-input>| Component | Supported constraints |
|---|---|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
<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>FormDatasgds-file-uploadselectedFiles<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>setInvalid(bool)invalidFeedbacknoValidate<sgds-input><sgds-textarea><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>novalidatenovalidate<form>setInvalid<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)| Parameter | Type | Description |
|---|---|---|
| | |
setInvalid(true)invalidFeedback| Component | Status |
|---|---|
| ✅ Implemented |
| ✅ Implemented |
| WIP |
| WIP |
| WIP |
| WIP |
| WIP |
| WIP |
| WIP |
<form>nameFormDatahasFeedbackhasFeedback="both"hasFeedbackinvalidFeedbacknew FormData(event.target)selectedFiles<sgds-file-upload><sgds-input><sgds-textarea>noValidatenovalidateelement.setInvalid(true/false)element.invalidFeedbacksgds-input<sgds-input><sgds-textarea>noValidatesetInvalidtype="reset"