By default in a Drupal 8 form submitform($form, FormStateInterface $form_state) will handle the the submission of form values. However we can also add our own submit handlers easily with Drupal 8 form api. The below example explains that.

Add the custom submit handler function with class name in the $form['#submit'] array


Code to add custom submit handler to Drupal 8 form

namespace Drupal\lnformcontrol\Form;

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

class CustomHandler extends FormBase {

    public function getFormId() {
        return 'lnformcontrol_customhandler';

    public function buildForm(array $form, FormStateInterface $form_state) {
        $form['name'] = array(
            '#type' => 'textfield',
            '#title' => $this->t("Enter Your Name"),
            '#size' => 50,
            '#maxlength' => 50,
            '#description' => $this->t("Enter your name to check the value"),

        $form['submit'] = array(
            '#type' => 'submit',
            '#value' => $this->t("submit"),
        $form['#submit'][] = '::custom_submit_handler';

        return $form;

   public function custom_submit_handler(array &$form, FormStateInterface $form_state) {
        drupal_set_message("Custom submit handler executed");
    public function submitForm(array &$form, FormStateInterface $form_state) {
        drupal_set_message('Default submit handler executed');



Leave a Reply

Your email address will not be published. Required fields are marked *