Answers.Body: <p>You can add a form rule to check the number of contacts in the group and if exceeded, display the error message.</p>
  
  <p>You might also be able to do this via js and the api, but that potentially could be circumvented. If you want to ensure this never happens, you should add a post hook on the civicrm_group_contact object and fatally abort if the constraint is violated.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-06-08T12:31:55.950Z
Answers.Text: This would need to be added for every ui (eg from the contact edit form, from the bulk action add to group, from the group tab...) and still won't prevent (for instance) to mass add them via import...
Answers.UserDisplayName: Xavier
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-06-08T13:05:41.883Z
Answers.Text: If you want to really restrict it, you can add a fatal abort to the post hook on the group contact object, which should be called every time
Answers.UserDisplayName: Donald Lobo
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-06-08T04:04:23.617Z
Answers.LastActivityDate: 2015-06-10T03:49:32.440Z
Answers.LastEditDate: 2015-06-10T03:49:32.440Z
Answers.UserDisplayName: Donald Lobo
Answers.Body: <p>Just to be sure: civi doesn't have any limit on the number of contacts per group, it's a business case you have that introduces that constraint, right?</p>
  
  <p>I didn't find a safe way to prevent an operation (I'm not sure it's a good idea, it might introduce lots of problems later on, the best IMO is to fail the query). </p>
  
  <p>One workaround to get that is to introduce an invalid value in the pre hook (eg set contact_id or group_id to null, the query will fail and it will prevent inserting.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-06-07T15:33:26.920Z
Answers.LastActivityDate: 2015-06-07T15:33:26.920Z
Answers.UserDisplayName: Xavier
Body: <p>I have a max no of users limit for a group, so whenever someone adds a groupContact using the civicrm dashboard, I would like to present them with an error which prevents adding a new group and stating a message for the same.</p>
  
  <p>I am using a civicrm_api() but could not figure out how to prevent the creation of groupcontact or provide a validation for the same?</p>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2015-06-08T19:58:37.283Z
Comments.Text: If you explain the purpose of this limit people might suggest other solutions
Comments.UserDisplayName: petednz - fuzion
Tags: <api><hooks><forms>
Title: How to limit number of contacts in a group?
ThreadText: # How to limit number of contacts in a group?
  
  (Asked by: username_0 on 2015-06-07)
  
  I have a max no of users limit for a group, so whenever someone adds a groupContact using the civicrm dashboard, I would like to present them with an error which prevents adding a new group and stating a message for the same.
  
  I am using a civicrm\_api() but could not figure out how to prevent the creation of groupcontact or provide a validation for the same?
  
  **Tags:** api, hooks, forms
  
  > **Comments:**
  > **username_1**: If you explain the purpose of this limit people might suggest other solutions  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_2 on 2015-06-08. Score: 2
  
  You can add a form rule to check the number of contacts in the group and if exceeded, display the error message.
  
  You might also be able to do this via js and the api, but that potentially could be circumvented. If you want to ensure this never happens, you should add a post hook on the civicrm\_group\_contact object and fatally abort if the constraint is violated.
  
  > **Comments:**
  > **username_3**: This would need to be added for every ui (eg from the contact edit form, from the bulk action add to group, from the group tab...) and still won't prevent (for instance) to mass add them via import...  
  > **username_2**: If you want to really restrict it, you can add a fatal abort to the post hook on the group contact object, which should be called every time  
  
  ---
  
  ### Answer by username_3 on 2015-06-07. Score: 2
  
  Just to be sure: civi doesn't have any limit on the number of contacts per group, it's a business case you have that introduces that constraint, right?
  
  I didn't find a safe way to prevent an operation (I'm not sure it's a good idea, it might introduce lots of problems later on, the best IMO is to fail the query).
  
  One workaround to get that is to introduce an invalid value in the pre hook (eg set contact\_id or group\_id to null, the query will fail and it will prevent inserting.
  
  
  ---
  
  