<?php
 /**
 * Implements hook_form_alter()
 */
function paypal_signup_form_alter(&$form, &$form_state, $form_id) { 
  if($form_id == 'user_register_form'){
      global $user;
      if(!in_array('administrator', array_values($user->roles))){
        //Submit handler for redirecting user to Paypal.
        $form['#submit'][] = '_paypal_signup_redirect_paypal';
    } else {
        // Remove validation when admin is creating user.
        foreach (element_children($form) as $key) {
            if (strpos($key, 'field_') !== FALSE) {
              $form[$key][LANGUAGE_NONE][0]['#required'] = FALSE;
              $form[$key][LANGUAGE_NONE]['#required'] = FALSE;
            }
         }
        dpm($form);
    } 
  }
}
?>

In the dpm it shows that #required = bool(false) also i tried with integer (0). But still i am getting errors of required fields.
It also shows the * sign on all custom fields which by the code it should not. any one please have a look why the validation still exists. If i go to manage fields on user entity and remove the uncheck the requird checkbox the fields set to be non-required. But i want that when admin is creating user the fields should not be required.

Thanks

Comments

Timo Vogt’s picture

Instead of set the required field to FALSE check if the field is empty if so just set a default value.

// Just a example (Not tested)
$form[$key][LANGUAGE_NONE]['#value'] = $form[$key][LANGUAGE_NONE]['#value'] ? NULL : 'default value' ;  
raheelwp’s picture

Plus there are some fields that take only INT some takes Var