Answers.Body: <p>You can use <a href="https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_validateForm/" rel="nofollow noreferrer">validateForm hook</a> to empty the errors on the input field. Eg</p>
  
  <pre><code>function hook_civicrm_validateForm($formName, &amp;$fields, &amp;$files, &amp;$form, &amp;$errors) {
    if (your_condition_statement) {
      $form-&gt;setElementError('field_id', NULL);
    }
  }
  </code></pre>
  
  <p>Also note that as this looks like a custom field, civi also provides a UI setting to disable the <code>required</code> attribute. Screenshot from <code>Add/Edit custom field</code> form.</p>
  
  <p><a href="https://i.sstatic.net/o9xWa.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/o9xWa.png" alt="enter image description here"></a></p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-11-27T06:59:38.970Z
Answers.LastActivityDate: 2018-11-27T06:59:38.970Z
Answers.UserDisplayName: jitendra - fuzion
Answers.Body: <p>Jitendra's answer is good.  I also like the <a href="https://github.com/twomice/com.joineryhq.profcond" rel="nofollow noreferrer">Profile Conditionals</a> extension by Joinery.  It only supports event and contribution pages at present, though it shouldn't be hard to extend it to work on others.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-11-28T04:14:41.877Z
Answers.LastActivityDate: 2018-11-28T04:14:41.877Z
Answers.UserDisplayName: Jon G - Megaphone Tech
Body: <pre><code>add_filter('civicrm_buildForm','contact_mark_t_fields_optional',10,2);
  function contact_mark_t_fields_optional($formName, &amp;$form){
      if($formName == 'CRM_Contact_Form_Contact'){
  
          $user = wp_get_current_user();
          $is_admin = FALSE;
          foreach($user-&gt;roles as $k =&gt;$v){
              if($v == 'administrator'){
                  $is_admin = TRUE;
              }
          }
          if ($is_admin){
              //Array ( [0] =&gt; custom_2_-1 [1] =&gt; custom_3_-1 [2] =&gt; custom_4_-1 [3] =&gt; custom_5_-1 [4] =&gt; custom_6_-1 [5] =&gt; custom_7_-1 [6] =&gt; custom_8_-1 [7] =&gt; custom_11_-1 [8] =&gt; custom_12_-1 [9] =&gt; custom_13_-1 [10] =&gt; custom_14_-1 [11] =&gt; custom_15_-1 )//
              $optional = array(
                  'custom_2_-1','custom_3_-1','custom_4_-1','custom_5_-1','custom_6_-1','custom_7_-1',
                  'custom_8_-1','custom_11_-1','custom_12_-1','custom_13_-1','custom_14_-1','custom_15_-1','custom_41_-1'
              );
              foreach($form-&gt;_required as $k =&gt; $v){
                  if (in_array($v, $optional)){
                      unset($form-&gt;_required[$k]);
                  }
  
              }
          }
  
      }
  
  }
  </code></pre>
  
  <p>I am trying to mark a checkbox field as optional programmatically when an admin views from the admin side, but while trying to save it, it gives a "Field Required Error"</p>
  
Tags: <wordpress>
Title: How to Mark CiviCRM checkbox field not required programmatically?
ThreadText: # How to Mark CiviCRM checkbox field not required programmatically?
  
  (Asked by: username_0 on 2018-11-27)
  
  add_filter('civicrm_buildForm','contact_mark_t_fields_optional',10,2);
  function contact_mark_t_fields_optional($formName, &$form){
      if($formName == 'CRM_Contact_Form_Contact'){
  
          $user = wp_get_current_user();
          $is_admin = FALSE;
          foreach($user->roles as $k =>$v){
              if($v == 'administrator'){
                  $is_admin = TRUE;
              }
          }
          if ($is_admin){
              //Array ( [0] => custom_2_-1 [1] => custom_3_-1 [2] => custom_4_-1 [3] => custom_5_-1 [4] => custom_6_-1 [5] => custom_7_-1 [6] => custom_8_-1 [7] => custom_11_-1 [8] => custom_12_-1 [9] => custom_13_-1 [10] => custom_14_-1 [11] => custom_15_-1 )//
              $optional = array(
                  'custom_2_-1','custom_3_-1','custom_4_-1','custom_5_-1','custom_6_-1','custom_7_-1',
                  'custom_8_-1','custom_11_-1','custom_12_-1','custom_13_-1','custom_14_-1','custom_15_-1','custom_41_-1'
              );
              foreach($form->_required as $k => $v){
                  if (in_array($v, $optional)){
                      unset($form->_required[$k]);
                  }
  
              }
          }
  
      }
  
  }
  
  I am trying to mark a checkbox field as optional programmatically when an admin views from the admin side, but while trying to save it, it gives a "Field Required Error"
  
  **Tags:** wordpress
  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_1 on 2018-11-27. Score: 4
  
  You can use validateForm hook to empty the errors on the input field. Eg
  function hook_civicrm_validateForm($formName, &$fields, &$files, &$form, &$errors) {
    if (your_condition_statement) {
      $form->setElementError('field_id', NULL);
    }
  }
  
  Also note that as this looks like a custom field, civi also provides a UI setting to disable the required attribute. Screenshot from Add/Edit custom field form.
  
  
  ---
  
  ### Answer by username_2 on 2018-11-28. Score: 0
  
  Jitendra's answer is good. I also like the [Profile Conditionals](https://github.com/twomice/com.joineryhq.profcond) extension by Joinery. It only supports event and contribution pages at present, though it shouldn't be hard to extend it to work on others.
  
  
  ---
  
  