Please Disable Your Browser Adblock Extension for our site and Refresh This Page!

our ads are user friendly, we do not serve popup ads. We serve responsible ads!

Refresh Page
Skip to main content
On . By CodimTh
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

Comments

Submitted by manasa (not verified) on Sun, 11/14/2021 - 10:27 Permalink

I have a config form where all the firlds in that form are required,where there is a button called save configeration ,where on click of that button its redirecting me to page called OTP page,but i have another button where on click of that button i want to redirect to login page,but i just want the login button to be clicked and want to redirect,without filling the form.so how do i do that.
Add new comment

Restricted HTML

Riadh Rahmi

Senior Web Developer (Drupal & Laravel)

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

Web Posts

Search

Page Facebook