Skip to main content
Category:

In this article, I'll show you how to add multiple submit buttons using Form API in drupal 8.

to add many submit buttons to your form, juste add "#submit" property to every element like this: 

  $form['actions']['previous'] = array(
      '#type' => 'submit',
      '#submit' => array([$this, 'previousForm']),
      '#value' => 'Previous',
      '#limit_validation_errors' => array(), //no validation for back button
  );


public static function previousForm(array &$form, FormStateInterface $form_state) {
    //TODO: your code
}

or you can use it like this '#submit' => array('::previousForm').

#submit: Specifies an alternate callback for form submission when the submit button is pressed. Use '::methodName' format or an array containing the object and method name (for example, [ $this, 'methodName'] ).

Example

<?php

namespace Drupal\codimth_form_api\Form;

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

/**
 * Implements a codimth Config Form API.
 */
class CodimthConfigForm extends ConfigFormBase
{


  /**
   * @return string
   */
  public function getFormId()
  {
    return 'codimth_form_api_admin_settings';
  }

  /**
   * Gets the configuration names that will be editable.
   *
   * @return array
   *   An array of configuration object names that are editable if called in
   *   conjunction with the trait's config() method.
   */
  protected function getEditableConfigNames()
  {
    return [
      'codimth_form_api.settings',
    ];
  }


  /**
   * @param array $form
   * @param FormStateInterface $form_state
   * @return array
   */
  public function buildForm(array $form, FormStateInterface $form_state)
  {
    $form['actions'] = [
      '#type' => 'actions',
    ];

    $form['actions']['submit1'] = [
      '#type' => 'submit',
      '#value' => $this->t('Submit One'),
      "#weight" => 1,
      '#submit' => array([$this, 'submitFormOne']),
      '#limit_validation_errors' => array()
    ];

    $form['actions']['submit2'] = [
      '#type' => 'submit',
      '#value' => $this->t('Submit Two'),
      "#weight" => 2,
      '#button_type' => 'primary',
      '#submit' => array([$this, 'submitFormTwo']),
      '#limit_validation_errors' => array()
    ];

    return parent::buildForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state)
  {
    //TODO: write your code
    parent::submitForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  public function submitFormPreOne(array &$form, FormStateInterface $form_state)
  {
    //TODO: write your code
  }

  /**
   * {@inheritdoc}
   */
  public function submitFormTwo(array &$form, FormStateInterface $form_state)
  {
    //TODO: write your code
  }
}

the result is like this:

Many submit buttons

Riadh Rahmi

Senior Web Developer PHP/Drupal & Laravel

I am a senior web developer, I have experience in planning and developing large scale dynamic web solutions especially in Drupal & Laravel.

Web Posts

Search

Page Facebook