adding new validation method using HookEventDispatcherInterface::FORM_ALTER event I'm trying to add a new validation method for 'user_login_form' using HookEventDispatcher module and events:


class MyFormEventsSubscriber implements EventSubscriberInterface {


public static function getSubscribedEvents() {
      return [ HookEventDispatcherInterface::FORM_ALTER => 'alterForm'];
  }


public function alterForm(FormAlterEvent $event) {

    if ($event->getFormId() == 'user_login_form') {

      $form = &$event->getForm();
      $form['#validate'][] = 'my_api_authentication';

    }
  }

  function my_api_authentication(&$form, FormStateInterface $form_state) {
    $test = 1;
  }
}


But i never reach my_api_authentication function. How i need to refer it and where should be suposed to be implemented ?

May be i should manage with hook_form_FORM_ID_alter instead FORM_ALTER HookEventDispatcherInterface event ?
You can do it that way too:
in Oriented object way don't call the method like 'my_api_authentication' it should be [$this, 'myApiAuthentication'] and also the method should be Camel Cases, try with the following:

class MyFormEventsSubscriber implements EventSubscriberInterface {

  public static function getSubscribedEvents() {
    return [HookEventDispatcherInterface::FORM_ALTER =&gt; 'alterForm'];
  }

  public function alterForm(FormAlterEvent $event) {
    if ($event-&gt;getFormId() == 'user_login_form') {
      $form = &amp;$event-&gt;getForm();
      $form['#validate'][] = [$this, 'myApiAuthentication'];
    }
  }
  function myApiAuthentication(&amp;$form, FormStateInterface $form_state) {
    // Your code validation here.
  }

}