How to rebuild form after AJAX call I'm attempting to allow the user to dynamically choose a number of fields based on a dropdown box using an ajax call, but I can't seem to get the ajax call to rebuild the form afterwards. 

&lt;?php
class AJAXexample extends BlockBase {
    public function blockForm($form, FormStateInterface $form_state) {
        if (empty($form_state-&gt;getValue('number'))) {
            $form_state-&gt;setValue('number', 3);
        } 
        $form['columnNum'] = [
            '#title'   =&gt; t('Number of Columns'),
            '#type'    =&gt; 'select',
            '#options' =&gt; [
                1         =&gt; '1',
                2         =&gt; '2',
                3         =&gt; '3',
                4         =&gt; '4',
            ],
            '#default_value' =&gt; $this-&gt;configuration['columnNum'],
            '#empty_option'  =&gt; t('-select-'),
            '#ajax'          =&gt; [
                'callback'      =&gt; [$this, 'columnCallback'],
            ],
        ];
        for ($i = 0; $i &lt; $form_state-&gt;getValue('number'); $i += 1) {
            $form['column'][$i] = [
                $i =&gt; [
                    '#type'       =&gt; 'details',
                    '#title'      =&gt; t('Column '.$numTitle),
                    '#open'       =&gt; FALSE,
                    'columnTitle' =&gt; [
                        '#type'      =&gt; 'textfield',
                        '#title'     =&gt; t('Column Title'),
                        '#value'     =&gt; $config[0]['columnTitle'],
                    ],  
                ],
            ];  
        return $form;
    }

    public function columnCallback(array &amp;$form, FormStateInterface $form_state) {
        $form_state-&gt;setValue('number', 10);
        $form_state-&gt;setRebuild(true);
        return $form;
    }
}


The number of textfields is based on the form_state variable 'number'. The callback columnCallback changes the form_state variable to 10, and is fired when the 'columnNum' form field is changed. However the form isn't rebuilt with the new number of fields even though $form_state->setRebuild(); is called. Is there a way to get the form to rebuild after an ajax call?

NOTE: I have already tried techniques such as replacing or appending the items of the form inside of the actual ajax call but when that happens none of the input into the replaced fields get passed to $form_state.

UPDATE:
After attempting 4k4's solution I receive an error

Recoverable fatal error: Argument 1 passed to Drupal\Core\Render\MainContent\AjaxRenderer::renderResponse() must be of the type array, null given, called in /Library/WebServer/Documents/aaep/web/core/lib/Drupal/Core/Form/FormAjaxResponseBuilder.php on line 89 and defined in Drupal\Core\Render\MainContent\AjaxRenderer-&gt;renderResponse() (line 45 of /Library/WebServer/Documents/aaep/web/core/lib/Drupal/Core/Render/MainContent/AjaxRenderer.php).


The belief is that the error occurs because $form['column'] is returning null despite being created as a container in the blockForm function. I've attempted calling the callback in other ways like

'#ajax' =&gt; [
    'callback' =&gt; '::columnCallback',
]


and 

'#ajax' =&gt; [
    'callback' =&gt; [$this, '\Drupal\my_examples\Plugin\Block\AJAXexample::columnCallback'],
]


But I receive the same error. Curiously, when I change the callback to return the entire $form instead of just $form['column'] it repeats the form (a copy of the form appears below the current form) and still without the proper number of columns.
First problem is to handle the value for the column number. On the first build get it from configuration, on a rebuild get it from the user input and put it in $columnNum.

The second is to decide, what part of the form changes in AJAX and put this in a div container with the id columns-wrapper.

class AJAXexample extends BlockBase {
    public function blockForm($form, FormStateInterface $form_state) {
        $columnNum = empty($form_state-&gt;getValue('columnNum')) ? $this-&gt;configuration['columnNum'] : $form_state-&gt;getValue('columnNum');
        $form['columnNum'] = [
            '#title'   =&gt; t('Number of Columns'),
            '#type'    =&gt; 'select',
            '#options' =&gt; [
                1         =&gt; '1',
                2         =&gt; '2',
                3         =&gt; '3',
                4         =&gt; '4',
            ],
            '#default_value' =&gt; $this-&gt;configuration['columnNum'],
            '#empty_option'  =&gt; t('-select-'),
            '#ajax'          =&gt; [
                'callback'      =&gt; [$this, 'columnCallback'],
                'wrapper'       =&gt; 'columns-wrapper', 
            ],
        ];
        $form['column'] = [
            '#type' =&gt; 'container',
            '#attributes' =&gt; ['id' =&gt; 'columns-wrapper'],
        ];
        for ($i = 0; $i &lt; $columnNum; $i += 1) {
            $form['column'][$i] = [
                $i =&gt; [
                    '#type'       =&gt; 'details',
                    '#title'      =&gt; t('Column '.$numTitle),
                    '#open'       =&gt; FALSE,
                    'columnTitle' =&gt; [
                        '#type'      =&gt; 'textfield',
                        '#title'     =&gt; t('Column Title'),
                        '#value'     =&gt; $config[0]['columnTitle'],
                    ],  
                ],
            ];  
        return $form;
    }


In the callback we only need to return the ajax wrapper.

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


Drupal rebuilds the form on every ajax request and puts it in the parameter $form of the callback. It would make no sense to try to rebuild it again.