I'm new to Drupal and I want to build a simple form. I get this error:

Fatal error: Class Drupal\medical\Form\LoginForm contains 1 abstract method and must therefore be declared abstract or implement the remaining methods (Drupal\Core\Form\FormInterface::submitForm) in C:\xampp\htdocs\Webdevelopment\Burst\Drupal_Test\modules\medical\src\Form\LoginForm.php on line 8

Here's the code:


namespace Drupal\medical\Form;

use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Form\FormBase;

class LoginForm extends FormBase {

public function getFormId(){
    return 'medical_login';
}

public function buildForm(array $form, FormStateInterface $form_state) {
    $form['username'] = array(
        '#title' => t('Username'),
        '#type' => 'textfield',
        '#required' => TRUE,
      );

      $form['password'] = array(
          '#title' => t('Password'),
          '#type' => 'password',
          '#required' => TRUE,
        );

      $form['submit'] = array(
        '#type' => 'submit',
        '#value' => 'Login',
      );

}

}

What does this error mean exactly and how do I fix this?

Thanks :)

Comments

Jaypan’s picture

Your form is extending FormBase, which implements FormInterface. When a class implements an interface, or (as in your case) extends an abstract class (like FormBase) that implements an interface, all methods defined in the interface must be implemented either in the abstract class, or the class that extends the abstract class.

FormInterface defines the function submitForm(), but neither the abstract class (FormBase), nor your class (LoginForm) that extends it, is implementing this method. This is why you are seeing the error.

You can fix this by copying the function definition from the interface, and adding it to your class. So add this to your class:

submitForm(array &$form, \Drupal\Core\Form\FormStateInterface $form_state) {}
Jaypan’s picture

validateForm() is not defined in the interface, so it's not required.

mmjvb’s picture

Haven't checked the actual code.

EDIT Checked the code and it is in the code as well. For both 8.3 and 8.4

<?php

namespace Drupal\Core\Form;

/**
 * Provides an interface for a Form.
 *
 * @ingroup form_api
 */
interface FormInterface {

  /**
   * Returns a unique string identifying the form.
   *
   * @return string
   *   The unique string identifying the form.
   */
  public function getFormId();

  /**
   * Form constructor.
   *
   * @param array $form
   *   An associative array containing the structure of the form.
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   *   The current state of the form.
   *
   * @return array
   *   The form structure.
   */
  public function buildForm(array $form, FormStateInterface $form_state);

  /**
   * Form validation handler.
   *
   * @param array $form
   *   An associative array containing the structure of the form.
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   *   The current state of the form.
   */
  public function validateForm(array &$form, FormStateInterface $form_state);

  /**
   * Form submission handler.
   *
   * @param array $form
   *   An associative array containing the structure of the form.
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   *   The current state of the form.
   */
  public function submitForm(array &$form, FormStateInterface $form_state);

}
Jaypan’s picture

Oh you're right, it is. My mistake.

mmjvb’s picture

Consider Module Development and Code Questions a more appropriate place for your support request, If you agree, would you mind moving it there. It is something only you can do.