Dynamically adding fields to form I'm trying to create a form in which you can fill in user names. I want to dynamically add new textfields by pressing a button. I based my code on the following question (because it also uses Drupal 8 and supposedly should work).
However, unfortunately it doesn't add the fields. The code I'm using currently looks like this:
&lt;?php
    /**
     * @file
     * Contains \Drupal\bulk_email\Form\EmailForm.
     */

    namespace Drupal\bulk_email\Form;
    use Drupal\Core\Form\FormBase;
    use Drupal\Core\Form\FormStateInterface;
    use Drupal\Component\Utility\UrlHelper;

    class EmailForm extends FormBase {
        public function getFormId() {
            return &quot;bulk_email_form&quot;;
        }

        public function buildForm(array $form, FormStateInterface $form_state) {
            $form['#tree'] = TRUE;

            $form['users'] = array(
              '#type' =&gt; 'fieldset',
              '#title' =&gt; t('Users'),
              '#prefix' =&gt; '&lt;div id=&quot;users-wrapper&quot;&gt;',
              '#suffix' =&gt; '&lt;/div&gt;',
            );

            $num_users = $form_state-&gt;getValue('num_users');
            if (empty($num_users)) {
                $num_users = 1;
            }
            for ($i = 0; $i &lt; $num_users; $i++) {
                $form['users'][$i] = array(
                    '#type'  =&gt; 'textfield',
                    '#description' =&gt; t('Select a user.'),
                );
            }

            $form['users']['users_more'] = array(
                '#type' =&gt; 'submit',
                '#value' =&gt; t('Add one'),
                '#submit' =&gt; array($this, 'bulk_email_addfieldsubmit'),
                '#ajax' =&gt; array(
                     'callback' =&gt; array($this, 'bulk_email_add_more_callback'),
                     'wrapper' =&gt; 'users-wrapper',
                ),
            );

            $form['actions'] = array('#type' =&gt; 'actions');
            $form['submit'] = array(
                '#type' =&gt; 'submit',
                '#value' =&gt; t('Send'),
            );
            return $form;
        }

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

        public function bulk_email_addfieldsubmit(array &amp;$form, FormStateInterface $form_state) {
            $num_users = $form_state-&gt;get('num_users') + 1;
            $form_state-&gt;set('num_users', $num_users);
            $form_state-&gt;setRebuild(TRUE);
        }

        public function bulk_email_add_more_callback(array &amp;$form, FormStateInterface $form_state) {
            return $form['users'];
        }
    }
?&gt;

I'm most likely missing a small detail, but I can't seem to find it...
Any help is appreciated.
Did you check if the submit is called in your ajax request?

public function bulk_email_addfieldsubmit(array &amp;$form, FormStateInterface $form_state) {
    $num_users = $form_state-&gt;get('num_users') + 1;
    $form_state-&gt;set('num_users', $num_users);


If not, you can put this code in buildForm(). In this case you need to check if the triggering element is the correct one.

And another small issue, this line in buildForm():

$num_users = $form_state-&gt;getValue('num_users');


Should be get('num_users'), because getValue() is for user input.