0

I have added fields to user entity and make them required and show on registration page. So the user signing up have to fill all the fields. On the admin side these all fields are coming but i don't want to make them required when admin is creating any user. I thought it will be solved if i hook_form_alter the user registration form and remove the #required attribute. I did this for admin and dpm($form). I set #required => 0 also unset #required but it keep on showing error of required field. I uncheck the required checkbox in field settings from admin panel and it does not show validation errors on both Admin and Frontend.

What i want is when admin create a user he just enter username and password and leave the rest of custom fields blank and it should register the user. But on the frontend users have to fill the whole form.

Below is the code:

    // If this is admin creating user
     foreach($form as $key => $value){
                if(strpos($key, 'field_') !== false){
                    $value['und']['#required'] = 0;
                    unset($value['und']['#required']); // This isn't working either
                }
            }
    // Endif

Updated Full Code:

/**
 * 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);
    } 
  }
}
1

Drupal has element_children() to help you safely loop over render arrays. Looping over such arrays manually will inevitably causes problems. Your code should look more like this:

foreach (element_children($form) as $key) {
  if (strpos($key, 'field_') !== FALSE) {
    $form[$key][LANGUAGE_NONE]['#required'] = FALSE;
  }
}

Incidentally, your current code doesn't work because you're modifying a variable that has only ben declared within that function's private scope.

  • but i dpm the $form after this loop and for every custom fields my code removed the #required key. – user38296 Nov 19 '14 at 13:56
  • Which is what you asked for...and if not, are you saying that the change you've made in the loop are not being persisted immediately after the loop? I highly doubt that, but if so it indicates you have a problem with your PHP installation. That isn't standard behaviour – Clive Nov 19 '14 at 13:56
  • I tried your code and its not working. By the way in dpm i don't see [LANGUAGE_NONE] any where. The structure i am gettting is key => array ('etc..','und' => array('#required => 1')); – user38296 Nov 19 '14 at 14:02
  • 1
    LANGUAGE_NONE == 'und'. I'm afraid I can't debug your site from here, you'll need to do that yourself. If you have more information about the problem, just edit the question. This code example is based on the assumption that you've already identified exactly where in the form array you need to manipulate the #required property. If you haven't already identified that, again you'll need to do so and update the question with those details – Clive Nov 19 '14 at 14:03
  • 1
    It might be something else - read this and see if that helps explain it – Clive Nov 19 '14 at 14:17
0

Before writing any custom code, I think you should try Profile2 Module and in addition Profile2_regpath Module provides you some Extra Options

0

I had the same issue with textfield. Drupal put the textfield added through GUI under a container and the rendered arrays look like this:

Array
(
    [#type] => container
    [#attributes] => Array
        (
            [class] => Array
                (
                    [0] => field-type-text
                    [1] => field-name-field-first-name
                    [2] => field-widget-text-textfield
                )

        )

    [#weight] => 5
    [#tree] => 1
    [#language] => und
    [und] => Array
        (
            [0] => Array
                (
                    [#entity_type] => user
                    [#entity] => stdClass Object
                        (
                            [uid] => 0
                            [hostname] => 0.0.0.0
                            [roles] => Array
                                (
                                    [1] => anonymous user
                                )

                            [cache] => 0
                        )

                    [#bundle] => user
                    [#field_name] => field_first_name
                    [#language] => und
                    [#field_parents] => Array
                        (
                        )

                    [#columns] => Array
                        (
                            [0] => value
                            [1] => format
                        )

                    [#title] => First Name
                    [#description] => 
                    [#required] => 1
                    [#delta] => 0
                    [#weight] => 0
                    [value] => Array
                        (
                            [#entity_type] => user
                            [#entity] => stdClass Object
                                (
                                    [uid] => 0
                                    [hostname] => 0.0.0.0
                                    [roles] => Array
                                        (
                                            [1] => anonymous user
                                        )

                                    [cache] => 0
                                )

                            [#bundle] => user
                            [#field_name] => field_first_name
                            [#language] => und
                            [#field_parents] => Array
                                (
                                )

                            [#columns] => Array
                                (
                                    [0] => value
                                    [1] => format
                                )

                            [#title] => First Name
                            [#description] => 
                            [#required] => 1
                            [#delta] => 0
                            [#weight] => 0
                            [#type] => textfield
                            [#default_value] => 
                            [#size] => 60
                            [#maxlength] => 255
                            [#attributes] => Array
                                (
                                    [class] => Array
                                        (
                                            [0] => text-full
                                        )

                                )

                        )

                )

            [#theme] => field_multiple_value_form
            [#field_name] => field_first_name
            [#cardinality] => 1
            [#title] => First Name
            [#required] => 1
            [#description] => 
            [#prefix] => 

            [#suffix] => 

            [#max_delta] => 0
            [#after_build] => Array
                (
                    [0] => field_form_element_after_build
                )

            [#language] => und
            [#field_parents] => Array
                (
                )

        )

    [#access] => 1
)

The element_children() doesn't help as it is not recursive. It loops only the immediate children whereas the textfield is rendered with three/four levels deep of arrays. The solution was to add this:

unset($form['field_first_name']['und']['#required']);
unset($form['field_first_name']['und'][0]['#required']);
unset($form['field_first_name']['und'][0]['value']['#required']);

Your Answer

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