1

I created a custom field on groups, which contains a string. When this string is not empty, I want it to be unique. What is the preferred way to do this? Should I implement a hook?

1 Answer 1

2

I think a hook's the way to go. For non-custom fields you can specify this a little more easily in the XML definition of the field, but for a custom field, I think the hook is your best best.

I think I'd use hook_validateForm or hook_pre. The first is more user-friendly, but since it's at the form level you have to identify everywhere the field can be edited. It's a fairly easy hook to write though - just make an API "get" call to CustomValue with that value and see if you get a count > 0.

3
  • Thanks! The documentation of hook_pre says 'This hook does not allow the abort of the operation, use a form hook instead.' So I guess I will go for hook_validateForm.
    – johanv
    Jul 29, 2015 at 10:17
  • On the other hand, this will not prevent setting duplicate values using the API...
    – johanv
    Jul 29, 2015 at 10:27
  • It's true that it won't abort the operation - but you CAN modify the values to ensure a bad value isn't written. Jul 29, 2015 at 13:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.