What is the best approach to enforce unique values in a Module?
What is the best approach to enforce unique values in a Module? For example, consider a list of shops with a field named 'ShopCode'. We want to make sure that the content editor can only enter unique ShopCodes.
One solution is to create a validation rule, such as 'UniqueShopCodeValidationRule', which checks if an item with the entered ShopCode already exists and apply this rule to the 'ShopCode' field using the field editor. A drawback is that the 'UniqueShopCodeValidationRule' could also be applied to other fields, although the advantage is that we can provide the content editor with a message if the shop code already exists.
With the IInfoObjectEventHandler we could also perform this check, but I don't see how we could display a message to the content editor indicating that the shop code already exists. Am I overlooking another possible approach?Describe your question...
Environment
Xperience by Kentico version: [30.6.0]
.NET version: [8|9]
Execution environment: [Azure]
Answers
A few questions
- How are new custom object types being created? (custom application UI, programmatically through background services, from visitors in website channels)
- Have you considered making your validation rule more generic by adding a validation rule properties class which can target different fields for uniqueness?
- The CreatePage and InfoEditPage are being used to insert new and edit the objects.
- This could indeed be a possibility, I will investigate this.
Thanks for thinking along!
For your #1 scenario you can customize the form submission handling in the create and edit page virtual methods, perform custom validation, and return a failed result in the case that the uniqueness constraint isn't fulfilled.
But option #2 is probably the best choice if it fits your requirements.
To answer this question, you have to login first.