Cannot get form_state storage or protected form values to work I am building a form using formBuilder. I have tried two methods to store values across form submit/validate for usage in ajax callbacks, but have been unsuccessful in either.
The first method, was to store a value in $form_state-&gt;set('name','value'); and retreiving it in the form submit/validate handler. That returned an empty result.
The second method was to store a variable on the class itself protected $image_deltas = 0; and then when the ajax form submit/validation is triggered, $this-&gt;image_deltas++; However, when I do this, it still does not work.
The following is the code I am using.
$form['images'] = [
  '#type' =&gt; 'container',
  '#id' =&gt; 'images',
];

dpm($this-&gt;image_deltas);

for ($i=0;$i&lt;=$this-&gt;image_deltas;$i++) {
  $form['images'][$i]['image'] = [
    '#type' =&gt; 'textfield',
    '#title' =&gt; t('Image Path:'),
  ];
}

$form['add_another'] = [
  '#type' =&gt; 'button',
  '#value' =&gt; t('Add Another Image'),
  '#ajax' =&gt; [
    'callback' =&gt; [$this, 'addAnotherImage'],
    'wrapper' =&gt; 'images',
  ],
  '#submit' =&gt; [$this, 'addAnotherImageSubmit'],
  '#limit_validation_errors' =&gt; [],
];

The form submission handler is the following.
public function addAnotherImageSubmit(&amp;$form, FormStateInterface $form_state) {
  $this-&gt;image_deltas++;
  $form_state-&gt;setRebuild();
}

The AJAX callback is the following one.
public function addAnotherImage(&amp;$form, FormStateInterface $form_state) {
  return $form['images'];
}

When I submit the form, dpm() prints 0 every time. The same thing happens if I use $form_state-&gt;set('image_deltas') and $form_state-&gt;get('image_deltas').
This:
'#submit' =&gt; [$this, 'addAnotherImageSubmit'],

Should be this:
'#submit' =&gt; [[$this, 'addAnotherImageSubmit']],

Submit handlers are only called for #type submit and not #type button, so you'll need to change the type.
You will also need to use $form_state-&gt;set(), rather than a property on the class. This is because the form class is re-instantiated for each iteration (build, validate, submit), and therefore values stored in the properties of the form class will not persist. $form_state-&gt;set() persists values, as these are stored in the database, and are repopulated for each instance of the form class (as long as you call $form_state-&gt;setRebuild() as you have done).