From 7edebbcef0c2ff4e08549af307e4e91e31b5d732 Mon Sep 17 00:00:00 2001 From: Hariom Balhara Date: Fri, 10 Mar 2023 19:41:41 +0530 Subject: [PATCH] Make sure that a hidden field/question is never required (#7652) * Update required field value as per hidden field value * Translate Required and Optional * Translate Label and Placeholder --- .../bookings/lib/getBookingResponsesSchema.ts | 3 ++- packages/features/form-builder/FormBuilder.tsx | 17 ++++++++++------- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/packages/features/bookings/lib/getBookingResponsesSchema.ts b/packages/features/bookings/lib/getBookingResponsesSchema.ts index e68a7f8a3f..c7fce8a68f 100644 --- a/packages/features/bookings/lib/getBookingResponsesSchema.ts +++ b/packages/features/bookings/lib/getBookingResponsesSchema.ts @@ -76,7 +76,8 @@ function preprocess({ : z.string().refine((val) => isValidPhoneNumber(val)); // Tag the message with the input name so that the message can be shown at appropriate place const m = (message: string) => `{${bookingField.name}}${message}`; - const isRequired = bookingField.required; + // If the field is hidden, then it can never be required + const isRequired = bookingField.hidden ? false : bookingField.required; if ((isPartialSchema || !isRequired) && value === undefined) { return; } diff --git a/packages/features/form-builder/FormBuilder.tsx b/packages/features/form-builder/FormBuilder.tsx index 6cdaaf5603..63c222e867 100644 --- a/packages/features/form-builder/FormBuilder.tsx +++ b/packages/features/form-builder/FormBuilder.tsx @@ -266,8 +266,7 @@ export const FormBuilder = function FormBuilder({ {fields.map((field, index) => { const fieldType = FieldTypesMap[field.type]; - // Hidden fields can't be required - const isRequired = field.required && !field.hidden; + const isRequired = field.required; if (!fieldType) { throw new Error(`Invalid field type - ${field.type}`); @@ -306,8 +305,12 @@ export const FormBuilder = function FormBuilder({ {field.label || t(field.defaultLabel || "")}
- {isRequired ? "Required" : "Optional"} - {field.hidden ? Hidden : null} + {field.hidden ? ( + // Hidden field can't be required, so we don't need to show the Optional badge + {t("hidden")} + ) : ( + {isRequired ? t("required") : t("optional")} + )} {Object.entries(groupedBySourceLabel).map(([sourceLabel, sources], key) => ( // We don't know how to pluralize `sourceLabel` because it can be anything @@ -444,13 +447,13 @@ export const FormBuilder = function FormBuilder({ required={!["system", "system-but-optional"].includes(fieldForm.getValues("editable") || "")} placeholder={t(fieldForm.getValues("defaultLabel") || "")} containerClassName="mt-6" - label="Label" + label={t("label")} /> {fieldType?.isTextType ? ( ) : null} @@ -474,7 +477,7 @@ export const FormBuilder = function FormBuilder({ onValueChange={(val) => { onChange(val); }} - label="Required" + label={t("required")} /> ); }}