Answers.Body: <p>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.</p>
  
  <p>I think I'd use <code>hook_validateForm</code> or <code>hook_pre</code>.  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.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-07-29T10:17:35.743Z
Answers.Text: Thanks! The documentation of [hook_pre](http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_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`.
Answers.UserDisplayName: johanv
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-07-29T10:27:40.567Z
Answers.Text: On the other hand, this will not prevent setting duplicate values using the API...
Answers.UserDisplayName: johanv
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-07-29T13:16:12.240Z
Answers.Text: It's true that it won't abort the operation - but you CAN modify the values to ensure a bad value isn't written.
Answers.UserDisplayName: Jon G - Megaphone Tech
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-07-28T22:27:26.100Z
Answers.LastActivityDate: 2015-07-28T22:27:26.100Z
Answers.UserDisplayName: Jon G - Megaphone Tech
Body: <p>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?</p>
  
Tags: <custom-fields><hooks>
Title: Enforce uniqueness of custom field
ThreadText: # Enforce uniqueness of custom field
  
  (Asked by: username_0 on 2015-07-28)
  
  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?
  
  **Tags:** custom-fields, hooks
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2015-07-28. Score: 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.
  
  > **Comments:**
  > **username_0**: Thanks! The documentation of [hook_pre](http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_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`.  
  > **username_0**: On the other hand, this will not prevent setting duplicate values using the API...  
  > **username_1**: It's true that it won't abort the operation - but you CAN modify the values to ensure a bad value isn't written.  
  
  ---
  
  