Input field not submitted in form when added through JS I have a form that is processed first by an external service through JS and afterwards adds an input field to the form:

$('#my-form').once('submit').submit(function(e) {
  // some processing
...
  form.append($('&lt;input type="hidden" name="token" /&gt;').attr('value', token.id));
  form.unbind();
  form.submit();
});


The field is added and also has the correct value. But when it is submitted the field is not present in neither my
 public function validateForm(array &amp;$form, FormStateInterface $form_state) nor in public function submitForm(array &amp;$form, FormStateInterface $form_state).

I already tried using a textfield instead of type hidden, and hide it with css, but same result
You can add a hidden field in buildForm() like below 

/**
 * {@inheritdoc}
 */
public function buildForm(array $form, FormStateInterface $form_state) {
  :
  :
  $form['token'] = [
    '#type' =&gt; 'hidden',
    '#default_value' =&gt; ''
  ]; 
  :
  :
}


or, if you're not building the form yourself, i.e. you're altering an already existing form, you can add the hidden field via hook_form_alter or hook_form_FORM_ID_alter.

Then update value of it using JS if needed