Skip to content

The open alternative to Salesforce, designed for AI.

Reveal hidden fields targeted by a validation error (#27476)

Thomas Trompette0546203683ee
diff --git a/packages/twenty-front/src/modules/side-panel/pages/record-creation-form/components/SidePanelRecordCreationFormPage.tsx b/packages/twenty-front/src/modules/side-panel/pages/record-creation-form/components/SidePanelRecordCreationFormPage.tsx
index 58f3ab88a5a..9fb2a949dc6 100644
--- a/packages/twenty-front/src/modules/side-panel/pages/record-creation-form/components/SidePanelRecordCreationFormPage.tsx
+++ b/packages/twenty-front/src/modules/side-panel/pages/record-creation-form/components/SidePanelRecordCreationFormPage.tsx
@@ -195,6 +195,17 @@ const SidePanelRecordCreationForm = ({
setValidationRuleViolations(draftViolations);
+ const isAnyViolationOnHiddenField = draftViolations.some((violation) =>
+ hiddenFieldMetadataItems.some(
+ (fieldMetadataItem) =>
+ fieldMetadataItem.id === violation.fieldMetadataId,
+ ),
+ );
+
+ if (isAnyViolationOnHiddenField) {
+ setRecordCreationFormAreHiddenFieldsShown(true);
+ }
+
if (draftViolations.length > 0) {
return;
}
diff --git a/packages/twenty-front/src/modules/side-panel/pages/record-creation-form/components/__tests__/SidePanelRecordCreationFormPage.test.tsx b/packages/twenty-front/src/modules/side-panel/pages/record-creation-form/components/__tests__/SidePanelRecordCreationFormPage.test.tsx
index fa6c0f4da9b..0c2d016e542 100644
--- a/packages/twenty-front/src/modules/side-panel/pages/record-creation-form/components/__tests__/SidePanelRecordCreationFormPage.test.tsx
+++ b/packages/twenty-front/src/modules/side-panel/pages/record-creation-form/components/__tests__/SidePanelRecordCreationFormPage.test.tsx
@@ -14,6 +14,7 @@ import {
jotaiStore,
resetJotaiStore,
} from '@/ui/utilities/state/jotai/jotaiStore';
+import { type ValidationRule } from '@/validation-rules/types/ValidationRule';
import {
FieldMetadataType,
PageLayoutTabLayoutMode,
@@ -62,7 +63,20 @@ const COMPANY_OBJECT = {
],
};
+const NICKNAME_RULE: ValidationRule = {
+ id: 'nickname-rule',
+ objectMetadataId: OBJECT_METADATA_ID,
+ name: 'Nickname is required',
+ description: null,
+ icon: null,
+ errorFieldMetadataId: NICKNAME_FIELD.id,
+ expression: 'isNonEmptyString(nickname)',
+ message: 'A company needs a nickname',
+ isActive: true,
+};
+
const settleRecordCreationDraft = jest.fn();
+let mockValidationRules: ValidationRule[] = [];
jest.mock('@/object-metadata/hooks/useObjectMetadataItemById', () => ({
useObjectMetadataItemById: () => ({ objectMetadataItem: COMPANY_OBJECT }),
@@ -89,7 +103,7 @@ jest.mock(
);
jest.mock('@/validation-rules/hooks/useValidationRules', () => ({
- useValidationRules: () => ({ validationRules: [] }),
+ useValidationRules: () => ({ validationRules: mockValidationRules }),
}));
jest.mock('@/ui/utilities/hotkey/hooks/useHotkeysOnFocusedElement', () => ({
@@ -210,6 +224,7 @@ describe('SidePanelRecordCreationFormPage', () => {
beforeEach(() => {
jest.clearAllMocks();
resetJotaiStore();
+ mockValidationRules = [];
seedRecordFormPageLayout();
jotaiStore.set(
recordCreationFormRequestComponentState.atomFamily({
@@ -289,4 +304,35 @@ describe('SidePanelRecordCreationFormPage', () => {
expect(screen.getByLabelText('Name')).toHaveValue('Apple');
expect(screen.getByLabelText('Nickname')).toHaveValue('Big Apple');
});
+
+ it('reveals a hidden field targeted by a validation error and keeps the message', async () => {
+ const user = userEvent.setup();
+
+ mockValidationRules = [NICKNAME_RULE];
+
+ renderPage();
+
+ await user.click(screen.getByTestId('record-creation-form-create-button'));
+
+ expect(screen.getByRole('alert')).toHaveTextContent(
+ 'A company needs a nickname',
+ );
+ expect(screen.getByLabelText('Nickname')).toBeInTheDocument();
+ expect(settleRecordCreationDraft).not.toHaveBeenCalled();
+ });
+
+ it('keeps hidden fields collapsed for a validation error on the whole record', async () => {
+ const user = userEvent.setup();
+
+ mockValidationRules = [{ ...NICKNAME_RULE, errorFieldMetadataId: null }];
+
+ renderPage();
+
+ await user.click(screen.getByTestId('record-creation-form-create-button'));
+
+ expect(screen.getByRole('alert')).toHaveTextContent(
+ 'A company needs a nickname',
+ );
+ expect(screen.queryByLabelText('Nickname')).not.toBeInTheDocument();
+ });
});
diff --git a/packages/twenty-front/src/modules/validation-rules/types/DraftValidationRuleViolation.ts b/packages/twenty-front/src/modules/validation-rules/types/DraftValidationRuleViolation.ts
index 2bcebad9a7c..03deec281d4 100644
--- a/packages/twenty-front/src/modules/validation-rules/types/DraftValidationRuleViolation.ts
+++ b/packages/twenty-front/src/modules/validation-rules/types/DraftValidationRuleViolation.ts
@@ -1,4 +1,5 @@
export type DraftValidationRuleViolation = {
ruleId: string;
message: string;
+ fieldMetadataId: string | null;
};
diff --git a/packages/twenty-front/src/modules/validation-rules/utils/__tests__/computeDraftValidationRuleViolations.test.ts b/packages/twenty-front/src/modules/validation-rules/utils/__tests__/computeDraftValidationRuleViolations.test.ts
index 092eed23a96..f127e992c07 100644
--- a/packages/twenty-front/src/modules/validation-rules/utils/__tests__/computeDraftValidationRuleViolations.test.ts
+++ b/packages/twenty-front/src/modules/validation-rules/utils/__tests__/computeDraftValidationRuleViolations.test.ts
@@ -107,6 +107,7 @@ describe('computeDraftValidationRuleViolations', () => {
{
ruleId: 'amount-rule',
message: 'A customer deal needs an amount',
+ fieldMetadataId: 'amount-field',
},
]);
});
@@ -246,4 +247,12 @@ describe('computeDraftValidationRuleViolations', () => {
),
).toEqual(['company-rule']);
});
+
+ it('should report a rule without an error field as a record-level violation', () => {
+ expect(
+ compute({ company: { employees: 3 } }, [COMPANY_RULE]).map(
+ (violation) => violation.fieldMetadataId,
+ ),
+ ).toEqual([null]);
+ });
});
diff --git a/packages/twenty-front/src/modules/validation-rules/utils/computeDraftValidationRuleViolations.ts b/packages/twenty-front/src/modules/validation-rules/utils/computeDraftValidationRuleViolations.ts
index 88a8546d741..14c86e89218 100644
--- a/packages/twenty-front/src/modules/validation-rules/utils/computeDraftValidationRuleViolations.ts
+++ b/packages/twenty-front/src/modules/validation-rules/utils/computeDraftValidationRuleViolations.ts
@@ -165,5 +165,6 @@ export const computeDraftValidationRuleViolations = ({
.map((validationRule) => ({
ruleId: validationRule.id,
message: validationRule.message,
+ fieldMetadataId: validationRule.errorFieldMetadataId ?? null,
}));
};