AJAX callback on form item generated by AJAX I'm trying to add AJAX callback to form element that was previously rendered by AJAX but with no luck. Code below. 

A bit of description. There are three main methods here:


buildForm - base form
extraField - AJAX callback attached to ['month']
choosePerson - AJAX callback attached to: ['week_day']['hour']


In the buildForm method I create base form with AJAX callback on 'month' select element. In the AJAX callback you can find an extraField method. This method contains form element called 'hour' ($form['week_day']['hour']) and this element renders well. But this element also contains AJAX callback, which doesn't work, it does not generate 'person_container' element and the 'person' select. 

Any idea how to ajaxify elements that previously were rendered by ajax?

class ReservationForm extends FormBase {

  public function getFormId() {
    return 'reservation_form';
  }

  public function buildForm(array $form, FormStateInterface $form_state) {

    $form['month'] = array(
      '#type' =&gt; 'select',
      '#title' =&gt; $this-&gt;t('Select month'),
      '#options' =&gt; array($this-&gt;t('January'), $this-&gt;t('February'), $this-&gt;t('March')),
      '#ajax' =&gt; array(
        'callback' =&gt; array($this, 'extraField'),
        'event' =&gt; 'change',
        'wrapper' =&gt; 'week-day',
      ),
    );

    // Disable caching on this form.
    $form_state-&gt;setCached(FALSE);

    $form['week_day'] = [
      '#type' =&gt; 'container',
      '#attributes' =&gt; ['id' =&gt; 'week-day'],
    ];
  }

  public function extraField(array &amp;$form, FormStateInterface $form_state) {
    $month = $form_state-&gt;getValue('month');

    $hour_options = array();

    if ($month == '0') {
      $hour_options[0] = $this-&gt;t('08:00');
      $hour_options[1] = $this-&gt;t('09:00');
      $hour_options[2] = $this-&gt;t('10:00');
    }

    if ($month == '1') {
      $hour_options[0] = $this-&gt;t('12:00');
      $hour_options[1] = $this-&gt;t('13:00');
      $hour_options[2] = $this-&gt;t('14:00');
    }

    if ($month == '2') {
      $hour_options[0] = $this-&gt;t('18:00');
      $hour_options[1] = $this-&gt;t('19:00');
      $hour_options[2] = $this-&gt;t('20:00');
    }

    $form['week_day']['hour'] = array(
      '#type' =&gt; 'select',
      '#title' =&gt; $this-&gt;t('Choose hours @month', array('@month' =&gt; $month)),
      '#options' =&gt; $hour_options,
      '#ajax' =&gt; array(
        // tried with that callback too: [ $this, 'choosePerson']
        'callback' =&gt; array($this, 'choosePerson'),
        'event' =&gt; 'change',
        'wrapper' =&gt; 'person',
      ),
    );

    $form['week_day']['person_container'] = array(
      '#type' =&gt; 'container',
      '#attributes' =&gt; ['id' =&gt; 'person-container'],
    );

    $form['week_day']['person_container']['person'] = array(
      '#type' =&gt; 'select',
      '#title' =&gt; $this-&gt;t('Choose person'),
      '#options' =&gt; ['John', 'Sarah', 'Peter'],
    );

    return $form['week_day'];
  }

  public function choosePerson(array &amp;$form, FormStateInterface $form_state) {
    $form['week_day']['person_container']['person'] = array(
      '#type' =&gt; 'select',
      '#title' =&gt; $this-&gt;t('Choose person'),
      '#options' =&gt; ['John', 'Sarah', 'Peter'],
    );

   return $form['week_day']['person_container'];
  }
}
The Link 4k4 posted in the discussion of the question gives the solution:


  Create a callback function (named by the #ajax['callback']). This is generally a very simple function which does nothing but select and return the portion of the form that is to be replaced on the original page. Note, that as part of the Form APIs security system, you cannot create new form elements in the callback function, as they will throw errors upon submission, and any #ajax on elements created in the callback will also not work. If you need to create new elements on ajax submit, they must be added in the form definition. You can use the values in $form_state to determine whether the form build process is the initial form load, or an #ajax initiated load.


So the following code example is working.

class ReservationForm extends FormBase {

  public function getFormId() {
    return 'reservation_form';
  }

  public function buildForm(array $form, FormStateInterface $form_state) {
    $form['month'] = [
      '#type' =&gt; 'select',
      '#title' =&gt; $this-&gt;t('Select month'),
      '#options' =&gt; [
        $this-&gt;t('January'),
        $this-&gt;t('February'),
        $this-&gt;t('March'),
      ],
      '#ajax' =&gt; [
        'callback' =&gt; [$this, 'extraField'],
        'event' =&gt; 'change',
        'wrapper' =&gt; 'week-day',
      ],
    ];

    // Disable caching on this form.
    $form_state-&gt;setCached(FALSE);

    $form['week_day'] = [
      '#type' =&gt; 'container',
      '#attributes' =&gt; ['id' =&gt; 'week-day'],
    ];

    if ($form_state-&gt;getUserInput()['_triggering_element_name'] == 'month') {
      $month = $form_state-&gt;getValue('month');
      $hour_options = [];

      if ($month == '0') {
        $hour_options[0] = $this-&gt;t('08:00');
        $hour_options[1] = $this-&gt;t('09:00');
        $hour_options[2] = $this-&gt;t('10:00');
      }

      if ($month == '1') {
        $hour_options[0] = $this-&gt;t('12:00');
        $hour_options[1] = $this-&gt;t('13:00');
        $hour_options[2] = $this-&gt;t('14:00');
      }

      if ($month == '2') {
        $hour_options[0] = $this-&gt;t('18:00');
        $hour_options[1] = $this-&gt;t('19:00');
        $hour_options[2] = $this-&gt;t('20:00');
      }

      $form['week_day']['hour'] = [
        '#type' =&gt; 'select',
        '#title' =&gt; $this-&gt;t('Choose hours @month', ['@month' =&gt; $month]),
        '#options' =&gt; $hour_options,
        '#ajax' =&gt; [
          'callback' =&gt; [$this, 'choosePerson'],
          'event' =&gt; 'change',
          'wrapper' =&gt; 'person-container',
        ],
      ];

      $form['week_day']['person_container'] = [
        '#type' =&gt; 'container',
        '#attributes' =&gt; ['id' =&gt; 'person-container'],
      ];

      $form['week_day']['person_container']['person'] = [
        '#type' =&gt; 'select',
        '#title' =&gt; $this-&gt;t('Choose person'),
        '#options' =&gt; ['John', 'Sarah', 'Peter'],
      ];
    }

    if ($form_state-&gt;getUserInput()['_triggering_element_name'] == 'hour') {
      $form['week_day']['person_container']['person'] = [
        '#type' =&gt; 'select',
        '#title' =&gt; $this-&gt;t('Choose person'),
        '#options' =&gt; ['Hans', 'Frank', 'Emma'],
      ];
    }

    return $form;
  }

  public function submitForm(array &amp;$form, FormStateInterface $form_state) {
  }

  public function extraField(array &amp;$form, FormStateInterface $form_state) {
    return $form['week_day'];
  }

  public function choosePerson(array &amp;$form, FormStateInterface $form_state) {
    return $form['week_day']['person_container'];
  }

}