-
Notifications
You must be signed in to change notification settings - Fork 2.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
#6094 Prevent creating a custom field with an existing name #6100
#6094 Prevent creating a custom field with an existing name #6100
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
PR Summary
- Added logic to prevent form submission if a custom field name already exists in
packages/twenty-front/src/pages/settings/data-model/SettingsObjectNewField/SettingsObjectNewFieldStep2.tsx
- Maintains a list of existing field names for validation
- Displays an error message when a duplicate name is detected
1 file(s) reviewed, no comment(s)
Edit PR Review Bot Settings
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
PR Summary
(updates since last review)
- Added error icon next to label input in
packages/twenty-front/src/modules/settings/data-model/fields/forms/components/SettingsDataModelFieldAboutForm.tsx
- Introduced state management for existing field names in
packages/twenty-front/src/pages/settings/data-model/SettingsObjectNewField/SettingsObjectNewFieldStep2.tsx
- Implemented validation checks to prevent duplicate field names in
packages/twenty-front/src/pages/settings/data-model/SettingsObjectNewField/SettingsObjectNewFieldStep2.tsx
- Disabled save button and displayed error message for duplicate field names in
packages/twenty-front/src/pages/settings/data-model/SettingsObjectNewField/SettingsObjectNewFieldStep2.tsx
2 file(s) reviewed, no comment(s)
Edit PR Review Bot Settings
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for your contribution @deval2498!
The code is functional but I added a few indications to try to follow our best practices!
useEffect(() => { | ||
if (!activeObjectMetadataItem) { | ||
navigate(AppPath.NotFound); | ||
} | ||
else { | ||
setExistingFieldNames(activeObjectMetadataItem.fields.map(field => field.name)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One of our best practices is to avoid using useEffect as much as possible as they trigger rerenders.
In our case here since the list of field names (activeObjectMetadataItem.fields.map(field => field.name))
is not supposed to change often we don't need to be reactive and to store them in a state :)
|
||
const formConfig = useForm<SettingsDataModelNewFieldFormValues>({ | ||
mode: 'onTouched', | ||
resolver: zodResolver(settingsFieldFormSchema), | ||
}); | ||
|
||
const [existingFieldNames, setExistingFieldNames] = useState<string[]>([]); | ||
const [validateNameField, setValidateNameField] = useState(true) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we want to try to use react-hook-form helpers instead of implementing a custom side-validation. Here it would mean relying on react-hook-form useForm's resolver (used L61): settingsFieldFormSchema can be updated to a function accepting additional limitations through arguments. This should prevent the user from saving as formConfig.formState.isValid will be false.
@@ -202,7 +219,10 @@ export const SettingsObjectNewFieldStep2 = () => { | |||
title="Name and description" | |||
description="The name and description of this field" | |||
/> | |||
<SettingsDataModelFieldAboutForm /> | |||
<SettingsDataModelFieldAboutForm isErrorField={!validateNameField}/> | |||
{!validateNameField && ( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here too, we should try to use react-hook-form to get the errors and display them at field-level
On it. Thanks for this review |
a1405c3
to
7677b01
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
PR Summary
(updates since last review)
- Added validation logic in
packages/twenty-front/src/pages/settings/data-model/SettingsObjectNewField/SettingsObjectNewFieldStep2.tsx
to prevent duplicate field names. - Updated
packages/twenty-front/src/modules/object-metadata/validation-schemas/fieldMetadataItemSchema.ts
to includeexistingLabels
parameter for label uniqueness validation. - Modified
packages/twenty-front/src/modules/object-metadata/validation-schemas/metadataLabelSchema.ts
to check for unique metadata labels. - Introduced maximum length constraints for field names in
packages/twenty-front/src/modules/settings/data-model/fields/forms/components/SettingsDataModelFieldAboutForm.tsx
. - Updated
packages/twenty-front/src/modules/settings/data-model/fields/forms/validation-schemas/settingsFieldFormSchema.ts
to acceptexistingLabels
for validation.
83 file(s) reviewed, 2 comment(s)
Edit PR Review Bot Settings
}, | ||
) | ||
.refine((label) => { | ||
try{if(!existingLabels) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🪶 style: Consider adding a space after try
for consistency and readability.
try{if(!existingLabels) { | |
try { if (!existingLabels) { |
const validateLabel = () => { | ||
trigger('label') | ||
} | ||
console.log(errors.label, "errors near label") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🪶 style: Remove console.log statement before merging.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
PR Summary
(updates since last review)
- Enhanced
metadataLabelSchema.ts
to ensure label uniqueness. - Removed
console.log
and added error message for non-unique labels inSettingsDataModelFieldAboutForm.tsx
.
No major changes found since last review.
2 file(s) reviewed, no comment(s)
Edit PR Review Bot Settings
great work @deval2498 , thanks for your contribution :) i allowed myself to add some minor improvements. there are some remaining issues from the tests (probably some code to adapt), do you want to have a look or shall i take over? @deval2498 |
Will take a look and update you on this, thanks for helping here |
Hi @deval2498, thanks for the PR! Could you remove the red error icon on the right side of the field and add a https://www.figma.com/design/xt8O9mFeLl46C5InWwoMrN/Twenty?node-id=3007-69739&t=hrDkyWQ3cs5iGjno-11 |
@ijreilly color exists in theme.border.color.danger, unable to perform tests on my machine heap running out of memory would be great if you can help:) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
great work @deval2498 this will be very useful ! :)
Fixes #6094
Description: Added logic inside SettingsObjectNewFieldStep2.tsx to prevent form submission
Current Behaviours: