Skip to content

Commit b46c04d

Browse files
fix(form-builder): show default label when field label is only whitespace (calcom#27793)
* fix(form-builder): show default label when field label is only whitespace * fix: add validation to ensure label is not empty or whitespace * fix: enhance label validation to prevent empty or whitespace labels in FormBuilder * fix: refactor label validation in FormBuilder to streamline whitespace checks * chore: retrigger CI (flaky vitest worker shutdown) --------- Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
1 parent dc43eba commit b46c04d

2 files changed

Lines changed: 25 additions & 2 deletions

File tree

apps/web/modules/event-types/components/tabs/advanced/FormBuilder.tsx

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,23 @@ const getLocationFieldType = (field: RhfFormField) => {
7070
return baseFieldType;
7171
};
7272

73+
function isWhitespaceOnly(val: unknown): boolean {
74+
return typeof val === "string" && val.length > 0 && val.trim().length === 0;
75+
}
76+
77+
function hasWhitespaceOnlyLabel(data: RhfFormField): boolean {
78+
if (isWhitespaceOnly(data.label)) return true;
79+
const variants = data.variantsConfig?.variants;
80+
if (!variants) return false;
81+
for (const variant of Object.values(variants)) {
82+
const fields = variant?.fields ?? [];
83+
for (const f of fields) {
84+
if (isWhitespaceOnly(f?.label)) return true;
85+
}
86+
}
87+
return false;
88+
}
89+
7390
/**
7491
* It works with a react-hook-form only.
7592
* `formProp` specifies the name of the property in the react-hook-form that has the fields. This is where fields would be updated.
@@ -387,6 +404,11 @@ export const FormBuilder = function FormBuilder({
387404
const type = data.type || "text";
388405
const isNewField = !fieldDialog.data;
389406

407+
if (hasWhitespaceOnlyLabel(data)) {
408+
showToast(t("label_cannot_be_whitespace"), "error");
409+
return;
410+
}
411+
390412
if (data.name === "guests" && type !== "multiemail") {
391413
showToast(t("guests_field_must_be_multiemail"), "error");
392414
return;
@@ -900,12 +922,12 @@ function FieldLabel({ field }: { field: RhfFormField }) {
900922
<span
901923
dangerouslySetInnerHTML={{
902924
// Derive from field.label because label might change in b/w and field.labelAsSafeHtml will not be updated.
903-
__html: markdownToSafeHTMLClient(field.label || t(field.defaultLabel || "") || ""),
925+
__html: markdownToSafeHTMLClient(field.label?.trim() || t(field.defaultLabel || "") || ""),
904926
}}
905927
/>
906928
);
907929
} else {
908-
return <span>{field.label || t(field.defaultLabel || "")}</span>;
930+
return <span>{field.label?.trim() || t(field.defaultLabel || "")}</span>;
909931
}
910932
}
911933
const variant = field.variant || defaultVariant;

packages/i18n/locales/en/common.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1231,6 +1231,7 @@
12311231
"additional_inputs": "Additional inputs",
12321232
"additional_input_description": "Require scheduler to input additional inputs prior the booking is confirmed",
12331233
"label": "Label",
1234+
"label_cannot_be_whitespace": "Label cannot be only whitespace",
12341235
"placeholder": "Placeholder",
12351236
"display_add_to_calendar_organizer": "Use \"Add to calendar\" email as the organizer",
12361237
"display_email_as_organizer": "We'll display this email address as the organizer, and send confirmation emails here.",

0 commit comments

Comments
 (0)