Answers.Body: <p>You will need to write small <a href="https://docs.civicrm.org/dev/en/latest/extensions/civix/" rel="nofollow noreferrer">extension</a> and then implement <a href="https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_validateForm/" rel="nofollow noreferrer">hook_civicrm_validateForm</a> to validate group field and then throw error. Example</p>
  
  <pre><code>/**
   * Implements hook_civicrm_validateForm().
   *
   * @param string $formName
   * @param array $fields
   * @param array $files
   * @param CRM_Core_Form $form
   * @param array $errors
   */
  function xxxx_civicrm_validateForm($formName, &amp;$fields, &amp;$files, &amp;$form, &amp;$errors) {
    if ('CRM_Contact_Form_Contact' == $formName) {
      if (!empty($fields['group']) &amp;&amp; !in_array(10, $fields['group'])) {
        $errors['group'] = ts('Group field contains invalid option.');
      }
    }
  }
  </code></pre>
  
  <p>HTH</p>
  
  <p>Pradeep</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-06-26T23:58:43.197Z
Answers.LastActivityDate: 2018-06-26T23:58:43.197Z
Answers.UserDisplayName: Pradeep Nayak
Body: <p>I need that when saving contact validate the entry of the group field.
  How can I do it?</p>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2018-06-27T00:49:00.477Z
Comments.Text: What sort of validation do you require?  What CMS are you using? If Drupal you may be able to do it using https://www.drupal.org/project/webform_validation and webform_civicrm
Comments.UserDisplayName: petednz - fuzion
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2018-06-27T08:22:32.717Z
Comments.Text: You refer to both 'set' and 'validate' which are different.  Can you expand a bit on what you are wanting to do?
Comments.UserDisplayName: Aidan
Tags: <groups>
Title: Set group field of a contact as required
ThreadText: # Set group field of a contact as required
  
  (Asked by: username_0 on 2018-06-26)
  
  I need that when saving contact validate the entry of the group field.
  How can I do it?
  
  **Tags:** groups
  
  > **Comments:**
  > **username_1**: What sort of validation do you require?  What CMS are you using? If Drupal you may be able to do it using https://www.drupal.org/project/webform_validation and webform_civicrm  
  > **username_2**: You refer to both 'set' and 'validate' which are different.  Can you expand a bit on what you are wanting to do?  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_3 on 2018-06-26. Score: 1
  
  You will need to write small [extension](https://docs.civicrm.org/dev/en/latest/extensions/civix/) and then implement [hook\_civicrm\_validateForm](https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_validateForm/) to validate group field and then throw error. Example
  
  ```
  /**
   * Implements hook_civicrm_validateForm().
   *
   * @param string $formName
   * @param array $fields
   * @param array $files
   * @param CRM_Core_Form $form
   * @param array $errors
   */
  function xxxx_civicrm_validateForm($formName, &$fields, &$files, &$form, &$errors) {
    if ('CRM_Contact_Form_Contact' == $formName) {
      if (!empty($fields['group']) && !in_array(10, $fields['group'])) {
        $errors['group'] = ts('Group field contains invalid option.');
      }
    }
  }
  
  ```
  
  HTH
  
  username_3
  
  
  ---
  
  