Form Ajax submission: erratic behavior I'm experiencing a weird behavior with an ajax form submit that I cannot find an answer to. 

This is my first attempt at making an ajax form so bear with me if I'm making a rookie mistake somewhere. 

namespace Drupal\MYMODULE\Forms;

use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Ajax\AjaxResponse;
use Drupal\Core\Ajax\HtmlCommand;



class InputForm extends FormBase {

  public function getFormID() {
    return 'authForm';
  }
  /**
   * Implements \Drupal\Core\Form\FormInterface::buildForm().
   */
  public function buildForm(array $form, FormStateInterface $form_state) {

    $form['#prefix'] = '&lt;div id="testdiv"&gt;&lt;/div&gt;';

    $form['key'] = array(
        '#type' =&gt; 'textfield',
        '#title' =&gt; t('Key'),
        '#size' =&gt; 10,
        '#name' =&gt; 'key',
        '#required' =&gt; TRUE,
        '#attributes' =&gt; ['id'=&gt;'key','name'=&gt;'key','class'=&gt;array('input-sm')]
    );

    $form['code'] = array(
        '#type' =&gt; 'textfield',
        '#title' =&gt; t('code'),
        '#name' =&gt; 'code',
        '#required' =&gt; TRUE,
        '#attributes' =&gt; ['id'=&gt;'code','name'=&gt;'code','class'=&gt;array('input-sm')]
    );

    $form['form_submit'] = array(
        '#type' =&gt; 'submit',
        '#value' =&gt; t('submit'),
        '#name' =&gt; 'submit',
        '#ajax' =&gt; [
            'callback' =&gt; '::test',
            'prevent' =&gt; 'submit',
            'progress' =&gt; []
        ]
    );
    return $form;
  }
  public function validateForm(array &amp;$form, FormStateInterface $form_state) {


      if (strlen($form_state-&gt;getValue('key')) &lt; 10) {
        $form_state-&gt;setErrorByName('key', $this-&gt;t('Key is too short'));
      }


  }

  public function submitForm(array &amp;$form, FormStateInterface $form_state) {
    // Handle submitted form data.
  }


  public function test(array &amp;$form, FormStateInterface $form_state) {
    if($form_state-&gt;hasAnyErrors()) {
        $form_state-&gt;setRebuild(TRUE);

        return($form);
    } 

    $response = new AjaxResponse();
    $response-&gt;addCommand(
        new HtmlCommand(
            '#testdiv',
            '&lt;p&gt;HELLO&lt;/p&gt;'
        )
    );
    return $response;

  }


}


So the erratic behavior is in the validation of the form. What is happening is when I click the submit button (with no values in the input fields) the ajax fires and through var dumping I know the $form_state-&gt;hasAnyErrors() returns true but the form rebuild that is returned is not modifying the form highlighting the fields with errors as expected. 

So after spending a couple hours trying to sort out this issue I got frustrated and mashed the submit button over and over again in a rage and low and behold it did what it's supposed to do after about eight clicks. 

So then I tried to narrow down how many times it would take hitting the button before it would work correctly and it would always happen at random clicks. Sometimes after 2, sometimes after 3... sometimes after 10. 

I'm stumped as to why it doesn't happen each and every single time the button is clicked rebuilding the form properly with errors. 

Of Note:


Have tried removing the prevent key 
Have tried with Bartik theme with same results 
Have tried setting an ID on the submit button but then ajax doesn't fire
Have confirmed that the form does have errors
Have input values into the fields (key value shorter than 10) and the same
behavior happens in the validateForm() function


If anyone could point me to what I could be doing wrong I'd very much appreciate it. Thanks in advance!

Edit for visual example:
Gif showing behavior
What you can see in the Gif looks more like the validation of the form in javascript not in ajax.

Normally you decide in the submit button by including the option 'wrapper' =&gt; 'ajax-wrapper-id', if you want to use the wrapper method and return in the ajax callback the new version of the form or parts of it, or use the response method and build an ajax response in the callback. Doubt that you can mix it like this.

If you use the wrapper method you don't need to rebuild the form, because it is already rebuilt, when it reaches the ajax callback.