How to write a partial "setRebuild"? I created a custom module which contains a Class Form. When I submit my form, I redirect the user to the form page. During the submission, I need to save a form_state variable to re-use it after the user is redirected to the form. 

So, from my submit method, I save my variable (with "$form_state-&gt;set(...)" ) and I add "$form_state-&gt;setRebuild()". 

All works perfectly. Now, I want preserve my variable AND empty some field's form. I don't want to preserve values of all fields of my form after the user has been redirected. 

I tried this : 

// From my buildForm
// ...
$form['test'] = [
  '#type' =&gt; 'textfield',
  '#title' =&gt; "test",
];
$form['submit'] = [
  '#type' =&gt; 'submit',
  '#value' =&gt; t('Submit'),
];
//...
// I check if my var exist, if yes, that means the user has submited the form and has been redirected to the form
if($form_state-&gt;has('results') &amp;&amp; $form_state-&gt;get('results')) {
  $form_state-&gt;setValueForElement($form['test'],"0");
}
return $form;


But I have this error when I use the setValueForElement method : 


  The website encountered an unexpected error. Please try again later.
  TypeError: Argument 1 passed to
  Drupal\Component\Utility\NestedArray::keyExists() must be of the type
  array, string given, called in
  /(...)/core/lib/Drupal/Core/Form/FormBuilder.php on line 1296 in
  Drupal\Component\Utility\NestedArray::keyExists() (line 259 of
  core/lib/Drupal/Component/Utility/NestedArray.php).


I don't understand where my mistake is, use the setValueForElement maybe not the better way? 

Edit (after Ismail Cherri's answer)

This is my new code : ("$form['#parents'] = array();" added)

// From my buildForm
// ...
$form['test'] = [
  '#parents' =&gt; [],
  '#type' =&gt; 'textfield',
  '#title' =&gt; "test",
];
$form['submit'] = [
  '#type' =&gt; 'submit',
  '#value' =&gt; t('Submit'),
];
//...
// I check if my var exist, if yes, that means the user has submited the form and has been redirected to the form
if($form_state-&gt;has('results') &amp;&amp; $form_state-&gt;get('results')) {
  $form_state-&gt;setValueForElement($form['test'],"0");
}
return $form;


I keep the same error when I submit my form...


  The website encountered an unexpected error. Please try again later.
  TypeError: Argument 1 passed to
  Drupal\Component\Utility\NestedArray::keyExists() must be of the type
  array, string given, called in
  /(...)/core/lib/Drupal/Core/Form/FormBuilder.php on line 1296 in
  Drupal\Component\Utility\NestedArray::keyExists() (line 259 of
  core/lib/Drupal/Component/Utility/NestedArray.php).
Can you please try adding this to your buildForm() method:

$form['#parents'] = array();


According to documentation: FormStateInterface::setValueForElement
You need to pass an array() and set the parents array for the form element.
Hope this helps. 

UPDATE
After revisiting the documentation, can you try this code:  

$form_state-&gt;setValueForElement($form_state-&gt;getValue('test'),"0");