According the link, I created a form in my hello_world module, but when I clear cache, the web site return 500 error. So what should I do? My hello_world module's codes is following:

hello_world.info.yml

name: Hello World Module
description: An experimental module to build our first Drupal 8 module
package: Test
type: module
version: 1.0
core: 8.x
configure: hello_world.admin_settings

hello_world.routing.yml

hello_world.content:
  path: '/helloworld'
  defaults:
    _controller: '\Drupal\hello_world\Controller\HelloWorldController::content'
    _title: 'HelloWorld!'
  requirements:
    _permission: 'access content'
hello_world.admin_settings:
  path: '/admin/config/helloworld'
  defaults:
    _form: '\Drupal\hello_world\Form\HelloWorldForm'
    _title: 'HelloWorld Form'
  requirements:
    _permission: 'administer site configuration'

hello_world.links.menu.yml

hello_world.admin:
  title: 'HelloWorld module settings'
  description: 'A basic module to return hello world'
  parent: system.admin_config_media
  route_name: hello_world.content
  weight: 100

HelloWorldForm.php

<?php
/**
 * @file
 * Contains \Drupal\hello_world\Form\HelloWorldForm.
 */

namespace Drupal\hello_world\Form;

use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;

/**
 * Defines a form that configures forms module settings.
 */
class HelloWorldForm extends ConfigFormBase{
	
	/**
	 * {@inherithoc}
	 */
	public function getFormId(){
		return 'hello_world_admin_settings';
	}
	
	/**
	 * {@inherithoc}
	 */
	public function buildForm(array $form,FormStateInterface &$form_state){

		$form['email'] = array(
			'#type' => 'email',
			'#title' => $this->t('Your .com email address.')
		);
		$form['show'] = array(
			'#type' => 'submit',
			'#value' => $this->t('Submit'),
		);
		return parent::buildForm($form, $form_state);
	}
	
	/**
	 * {@inheritdoc}
	 */
	public function validateForm(array &$form, FormStateInterface &$form_state) {
	
		if (strpos($form_state['values']['email'], '.com') === FALSE ) {
			$this->setFormError('email', $form_state, $this->t('This is not a .com email address.'));
		}
	}
	
	/**
	 * {@inheritdoc}
	 */
	public function submitForm(array &$form, FormStateInterface &$form_state) {
	
		drupal_set_message($this->t('Your email address is @email', array('@email' => $form_state['values']['email'])));
	}
}
?>

HelloWorldController.php

<?php
/**
@file
Contains \Drupal\hello_world\Controller\HelloWorldController.
*/

namespace Drupal\hello_world\Controller;

use Drupal\Core\Controller\ControllerBase;

class HelloWorldController extends ControllerBase {

	public function content() {
		return array(
				'#type' => 'markup',
				'#markup' => t('Hello world'),
		);
	}
}
?>

Comments

joshi.rohit100’s picture

Code seems fine to me except one thing - You are passing form_state as reference which is wrong as it is already an object.
Can you please check the logs and provide what exactly is the error. Also don't know but try with remove links.menu.yml file and then try. Also please can you mention your module structure and location.

bluefireray’s picture

Thanks Rohit Joshi replies!
I modified the HelloWorldForm.php to fix syntax error and remove the links.menu.yml and try again. The 500 error still repro.
My module structure and location:
structure
hello_world
src
Controller
HelloWorldController.php
Form
HelloWorldForm.php
hello_world.info.yml
hello_world.links.menu.yml
hello_world.routing.yml
location
drupal/modules/

joshi.rohit100’s picture

I tried your code and its working fine. You just need to take care 2 things -

1. $form_state should not be passed by reference as I mentioned in earlier comment.
2. you need to implement the getEditableConfigNames method as this method is abstract.

You can replace your Form code with this :-

<?php
/**
 * @file
 * Contains \Drupal\hello_world\Form\HelloWorldForm.
 */

namespace Drupal\hello_world\Form;

use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;

/**
 * Defines a form that configures forms module settings.
 */
class HelloWorldForm extends ConfigFormBase{
    
    /**
     * {@inherithoc}
     */
    public function getFormId(){
        return 'hello_world_admin_settings';
    }

    function getEditableConfigNames() {
      return ['hello_world.settings'];
    }
    
    /**
     * {@inherithoc}
     */
    public function buildForm(array $form,FormStateInterface $form_state){

        $form['email'] = array(
            '#type' => 'email',
            '#title' => $this->t('Your .com email address.')
        );
        
        return parent::buildForm($form, $form_state);
    }
    
    /**
     * {@inheritdoc}
     */
    public function validateForm(array &$form, FormStateInterface $form_state) {
    
        
    }
    
    /**
     * {@inheritdoc}
     */
    public function submitForm(array &$form, FormStateInterface $form_state) {
    
        drupal_set_message($this->t('thanks'));
    }
}
?>
bluefireray’s picture

Thanks Rohit Joshi replies! Now, it can work well!