Answers.Body: <p>Welcome to SE, You can set contact type mandatory by using <a href="https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_buildForm/" rel="nofollow noreferrer">hook_civicrm_buildForm</a> something like below</p>
  
  <pre><code>function extension_civicrm_buildForm($formName, &amp;$form) {
    if ('CRM_Contact_Form_Contact' == $formName
      &amp;&amp; !($form-&gt;getVar('_action') &amp; CRM_Core_Action::DELETE)
    ) {
      $form-&gt;addRule('contact_sub_type',  ts('This field is required.'), 'required');
    }
  }
  </code></pre>
  
  <p>HTH</p>
  
  <p>Pradeep</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-02-15T16:23:46.930Z
Answers.Text: <?php
  function civicrm_hooks_civicrm_buildForm($formName, &$form) {
    if ($formName == 'CRM_Contact_Form_Edit_Individual') {
        $form->addRule('contact_sub_type', ts('This field is required.'), 'required');
    }
  }  
  
  is what i have so far but I cant get it to work :( any ideas?
Answers.UserDisplayName: user6861
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-02-15T16:28:01.270Z
Answers.Text: I have managed to set up the hook in drupal etc, and I did see my hook on the page (i forgot the <?php initially)
Answers.UserDisplayName: user6861
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-02-15T16:29:07.130Z
Answers.Text: Is it working or not?
Answers.UserDisplayName: Pradeep Nayak
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-02-15T16:31:26.117Z
Answers.Text: Its not working - I have successfully set the hooks up but the hook I have written does not work.
Answers.UserDisplayName: user6861
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-02-15T16:31:49.750Z
Answers.Text: What is the module or extension name where you have added above code?
Answers.UserDisplayName: Pradeep Nayak
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-02-15T16:33:44.043Z
Answers.Text: It's in a custom module called civicrm_hooks that has been enabled.
Answers.UserDisplayName: user6861
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-02-15T16:35:02.450Z
Answers.Text: It should be CRM_Contact_Form_Contact not CRM_Contact_Form_Edit_Individual
Answers.UserDisplayName: Pradeep Nayak
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-02-15T15:18:23.000Z
Answers.LastActivityDate: 2019-02-15T15:18:23.000Z
Answers.UserDisplayName: Pradeep Nayak
Body: <p>I want to make it so that all new contacts <em>must have</em> a contact type. I have found where the contact type is in the templates, and have managed to add the wee * besides it, but I am struggling to find what tag or code I need to add to make it be an actual mandatory field. Is there some way of doing this? </p>
  
Tags: <templates><user><custom-templates>
Title: How to force a field to be mandatory?
ThreadText: # How to force a field to be mandatory?
  
  (Asked by: username_0 on 2019-02-15)
  
  I want to make it so that all new contacts *must have* a contact type. I have found where the contact type is in the templates, and have managed to add the wee \* besides it, but I am struggling to find what tag or code I need to add to make it be an actual mandatory field. Is there some way of doing this?
  
  **Tags:** templates, user, custom-templates
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2019-02-15. Score: 2 | Accepted answer
  
  Welcome to SE, You can set contact type mandatory by using [hook\_civicrm\_buildForm](https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_buildForm/) something like below
  
  ```
  function extension_civicrm_buildForm($formName, &$form) {
    if ('CRM_Contact_Form_Contact' == $formName
      && !($form->getVar('_action') & CRM_Core_Action::DELETE)
    ) {
      $form->addRule('contact_sub_type',  ts('This field is required.'), 'required');
    }
  }
  
  ```
  
  HTH
  
  username_1
  
  > **Comments:**
  > **username_0**: <?php function civicrm_hooks_civicrm_buildForm($formName, &$form) {   if ($formName == 'CRM_Contact_Form_Edit_Individual') {       $form->addRule('contact_sub_type', ts('This field is required.'), 'required');   } }    is what i have so far but I cant get it to work :( any ideas?  
  > **username_0**: I have managed to set up the hook in drupal etc, and I did see my hook on the page (i forgot the <?php initially)  
  > **username_1**: Is it working or not?  
  > **username_0**: Its not working - I have successfully set the hooks up but the hook I have written does not work.  
  > **username_1**: What is the module or extension name where you have added above code?  
  > **username_0**: It's in a custom module called civicrm_hooks that has been enabled.  
  > **username_1**: It should be CRM_Contact_Form_Contact not CRM_Contact_Form_Edit_Individual  
  
  ---
  
  