Update a container field with ajax callback breaks checkboxes and radios

Created on 30 June 2016, about 9 years ago
Updated 6 September 2024, 10 months ago

Hi all.

I have a simple form with a textfield with autocomplete that update with his value a field container to display a checkboxes field. The container will be updated but do not display the checkboxes or radios. Select type field works fine.

/**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $form['company_name'] = [
      '#type' => 'textfield',
      '#title' => t('1: Find a Company by Name'),
      '#autocomplete_route_name' => 'ect_company.find_company_by_name_controller_findByName',
      '#autocomplete_route_parameters' => [
        'type' => 'broker',
      ],
      '#ajax' => [
        'callback' => array($this, 'findUsers'),
        'event' => 'autocompleteselect',
        'wrapper' => 'users-wrapper',
        'progress' => array(
          'type' => 'throbber',
          'message' => t('Searching Users...'),
        ),
      ],
    ];

    $form['users_wrapper'] = [
      '#type' => 'container',
      '#attributes' => ['id' => 'users-wrapper'],
    ];

    $form['#attached']['library'][] = 'ect_event/drupal.ect_event_add_user.form';
    $form_state->setCached(FALSE);

    return $form;
  }


  /**
   * Ajax callback to find company users.
   */
  public function findUsers(array &$form, FormStateInterface $form_state) {

    $companyName = $form_state->getValue('company_name');
    if (preg_match('/\((\d+)\)/', $companyName, $matches)) {
      $companyId = $matches[1];

      $schedulerData = [
        [
          "value" => "11",
          "label" => "Tivo Preserva",
        ],
        [
          "value" => "90",
          "label" => "Ente Fet",
        ]
      ];

      foreach ($schedulerData as $item) {
        $options[$item['value']] = $item['label'];
      }

      $form['users_wrapper']['users'] = [
        '#type' => 'checkboxes',
        '#title' => t('2: Select users to add from the Company Members'),
        '#options' => $options,
      ];
    }
    else {
      // Create an Ajaxcommand response to display the message
      $message = $this->t('Error on retrieve company id');
    }

    return $form['users_wrapper'];
  }
💬 Support request
Status

Closed: works as designed

Version

11.0 🔥

Component
Form 

Last updated 32 minutes ago

Created by

🇮🇹Italy vincenzodb

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • 🇳🇿New Zealand quietone

    It looks like this has been answered in #11 and #22. Therefore closing.

    The Drupal Core issue queue is not the ideal place for support requests (that option is mostly there for filing support issues for contributed modules and themes). There are several support options listed on our support page (Community > Support at the top of Drupal.org) and there is Drupal Slack . Drupal Slack and the Forums, which are our two main support mechanisms in the Drupal community.

  • Status changed to Closed: works as designed 10 months ago
Production build 0.71.5 2024