2

I am trying to create a custom form in my drupal 8 instance, and I have followed a few different tutorials, but every time I try to go to the forms path I see the following error on the page:

The website encountered an unexpected error. Please try again later.

When I check my apache log, I see it came because of this error:

Uncaught PHP Exception InvalidArgumentException: "Class "Drupal\report_service_call\Form\ReportServiceCallForm" does not exist." at /var/www/html/abcnj/core/lib/Drupal/Core/DependencyInjection/ClassResolver.php line 24

I do have my ReportServiceCallForm set up at the following path under my module:

lib/Drupal/report_service_call/Form/ReportServiceCallForm.php

this is the contents of that file:

<?php
/**
 *@file 
  *Contains /Drupal/report_service_call/Form/ReportServiceCallForm
  */


  namespace Drupal\report_service_call\Form;

  use Drupal\Core\Form\FormBase;
  use Drupal\Core\Form\FormStateInterface;
 /**
  *Implements Drupal form Interface
  */
 class ReportServiceCallForm extends FormBase{

 /**
  *{@inheritdoc}
  */
  public function getFormID(){     
     return 'report_service_call_form';
  }
 /**
  *{@inheritdoc}
  */
public function buildForm(array $form, array &$form_state){
  $form['company_name'] = array(
    '#type'=> 'textfield',
    'title'=> $this->t('Company Name')
  );

$form['cotact_name'] = array(
  '#type'=> 'textfield',
  'title'=> $this->t('Contact Name')
);

$form['current_system_type'] = array(
  '#type'=> 'textfield',
  'title'=> $this->t('Current System Type')
);

$form['telephone_number'] = array(
  '#type'=> 'tel',
  'title'=> $this->t('Telephone Number')
);

$form['email'] = array(
  '#type'=> 'email',
  'title'=> $this->t('Email Address')
);

$form['service_request'] = array(
  '#type'=> 'textfield',
  'title'=> $this->t('Service Request')
);

$form['actions']['#type'] = 'actions';
$form['actions']['submit'] = array(
    '#type' => 'submit',
    '#value' => $this->t('Save'),
    '#button_type'=>'primary',
);
return $form;
}

  /**
  *{@inheritdoc}
  */
  public function validateForm(array &$form, array &$form_state){

  }

 /**
  *{@inheritdoc}
  */
  public function submitForm(array &$form, array &$form_state){

  }
}

Also I have the contents of my routing.yml as follows:

report_service_call.form:
    path: '/Customer-Service/Report-Service-Call'
defaults:
  _title: 'Report Service Call Form'
  _form: 'Drupal\report_service_call\Form\ReportServiceCallForm'
requirements:
  _permission: 'access content'

1 Answer 1

5

The file needs to be located at [MODULE FOLDER]/src/Form/ReportServiceCallForm.php

1
  • You were right, I changed to this and it worked. Must have been using outdated documentation/tutorials. Thanks!
    – atg963
    Commented May 29, 2017 at 19:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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