1

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 ?

0

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 => 'alterForm'];
  }

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

}
  • works like a charm!. Thanks @berramou. I'm new in PHP world, where can i learn this syntaxis? : [$this, 'string'] . It's new for me :-) – fuegonju Jan 19 at 8:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.