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(
            foreach($form->_required as $k => $v){
                if (in_array($v, $optional)){




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"


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.

enter image description here


Jitendra's answer is good. I also like the Profile Conditionals 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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.