How can you contact us

2By phone at +40 749 014 360

If you still have problems, please let us know, by sending an email to info@westweb-solutions.com Thank you!

OFFICE HOURS

Mon-Fri 9:00AM - 6:00AM
Sat - 9:00AM - 1:00PM
Sundays by appointment only!

Drupal 8 - How to attach custom validate/submit to an existing form

/ / Drupal 8

In order to attach custom validate/submit callbacks to an existing form in Drupal 8, you need to alter it.

To alter an existing form, we need to use the hook_form_alter() function.

Let's suppose for example that we have an existing form called example_form defined in a module example.

An we have a module called mymodule, where we want to alter the example_form and add our custom validate/submit callbacks.

The first step is to implement the hook_form_alter() in your mymodule.module file.


  /**
   * Implements hook_form_alter().
   */
  function mymodule_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
    // Your code goes here.
  }


The $form_id variable will contain the id of the form. In our case, we will check if $form_id == 'example_form' to attach the validate/submit only to example_form.

There are two ways to attach a custom validate/submit callbacks: procedural (similar to Drupal 7) or object oriented.

In the object oriented way, we need to have a class and a static method that will be called on validate/submit.


  /**
   * Implements hook_form_alter().
   */
  function mymodule_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
    if ($form_id == 'example_form') {
      // Procedural call
      $form['#validate'][] = 'mymodule_example_form_validate';
      $form['#submit'][] = 'mymodule_example_form_submit';

      // OOP Call: Use a static function from ExampleController class.
      $form['#validate'][] = 'Drupal\mymodule\Controller\ExampleController::example_form_validate';
      $form['#submit'][] = 'Drupal\mymodule\Controller\ExampleController::example_form_submit';
    }
  }


In this case, the methods example_form_validate() and example_form_submit() exists in ExampleController class and are static methods.


  /**
   * Validate the example_form.
   *
   * @param array $form
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   */
  public static function example_form_validate(&$form, FormStateInterface $form_state) {
    dsm('Validate from controller class');
  }

  /**
   * Submit the example_form.
   *
   * @param array $form
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   */
  public static function example_form_submit(&$form, FormStateInterface $form_state) {
    dsm('Submit from controller class');
  }


You can replace controller class with a service class and it will work in the same way.

For the procedural call, we need to declare the mymodule_example_form_validate() and mymodule_example_form_submit() functions in mymodule.module file.


  /**
   * Validate callback for example_form.
   *
   * @param array $form
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   */
  function mymodule_example_form_validate(&$form, FormStateInterface $form_state) {
    dsm('Validate from .module');
  }

  /**
   * Submit callback for example_form.
   *
   * @param array $form
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   */
  function mymodule_example_form_submit(&$form, FormStateInterface $form_state) {
    dsm('Submit from .module');
  }


Next, I will show you how to pass data between validate/submit callbacks.

Sign in your account to have access to different features

Forgot your details?

TOP