0

I have a form element as follows :

$form['fieldset']['select-user'] = array(
            '#type' => 'select',
            '#title' => t('Select a user'),
            '#options' => select_user_dropdown_options(),
            '#description' => t('Select a user'),
            '#size' => 10,
            '#attributes' => array(
                'style' => 'width:600px',
                'multiple' => 'true'
            )
        );

And in the form submit handler i am processing it as follows :

    /**
     * #submit callback for node_adoption_transfer_form().
     */
    public function submitForm(array &$form, FormStateInterface $form_state) {
        $groupId = $form_state->getValue('select-user-group');
        $groupName = $form_state->getValue('group_name');
        $userId = $form_state->getValue('select-user');
        $groupName = empty($groupName) ? '' : $groupName;
        #print_r($userId);exit;
        foreach ($userId as $key => $value) {
            $query = "select * from {usergroup_user_map} where user_id=$value AND group_id ='$groupId'";
            if (count(db_query($query)->fetchObject()) > 0) {
                continue;
            } else
                db_query("insert into {usergroup_user_map} (user_id,group_name,group_id) values ($value,'$groupName','$groupId')");
        }
     }

But when am printing the $userId its only printing the last value , How to resolve this ?

UPDATE

my issue solved .I was using '#multiple' => 'true' instead of '#multiple' => TRUE , my bad ;) . That saved my day ..

4

I had same issue , but i solved like this

$form['fieldset']['select-user'] = array(
   '#type' => 'select',
   '#title' => t('Select a user'),
   '#options' => select_user_dropdown_options(),
   '#description' => t('Select a user'),
   '#size' => 10,
   '#multiple' => TRUE,
   '#attributes' => array(
      'id' => 'edit-select-user',
      'style' => 'width:600px',
   )
);

Code: enter image description here

Form: enter image description here

Output in submit:

enter image description here

  • It doesnt work , even multiple property of select box is not working – Crazyrubixfan Aug 11 '16 at 9:59
  • @icecreamJelly: updated answer with screenshots, refer that may be this will help. And also clear cache if possible. – Ajay Reddy Aug 11 '16 at 10:06
  • I checked , If its not there in attributes array , its not acting as multi select at all – Crazyrubixfan Aug 11 '16 at 12:30
  • User error: "multiple" is an invalid render array key in Drupal\Core\Render\Element::children() (line 102 of core/lib/Drupal/Core/Render/Element.php). – Crazyrubixfan Aug 12 '16 at 8:42
  • Using drupal 8.1.8 version and I am using this in EntityForm. – Ajay Reddy Aug 12 '16 at 9:33

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.