Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
By perryjanssen on
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
Your form is extending
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:
Looks like validateForm is missing as well
See http://valuebound.com/resources/blog/step-by-step-method-to-create-a-cus... for details
validateForm() is not defined
validateForm() is not defined in the interface, so it's not required.
According to the documentation it is!
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
Oh you're right, it is. My
Oh you're right, it is. My mistake.
Request for moving
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.