Score:-2

Can I use two different forms in one module?

cn flag

I'm new to Drupal 8 and I was wondering if I can use two forms which are inside the Form folder(mymodule/src/Form/). The name of my forms are FormOne.php and FormTwo.php.

I can only display and use FormOne.php through my entire module.

I have researched it and can't find an answer.

Can you help me on displaying the second form?

Score:2
ru flag

You can create a custom Form in MyCustomFormA.php

<?php

namespace Drupal\MY_MODULE\Form;

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

/**
 * Class MyCustomFormA.
 */
class MyCustomFormA extends FormBase {

  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return 'my_custom_form_a';
  }
.....
}

Same way you can create another Form in MyCustomFormB.php

<?php

namespace Drupal\MY_MODULE\Form;

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

/**
 * Class MyCustomFormB.
 */
class MyCustomFormB extends FormBase {

  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return 'my_custom_form_b';
  }
....
}

In your MY_MODULE.routing.yml

MY_MODULE.my_custom_form_a:
  path: '/MY_MODULE/my_custom_form_a'
  defaults:
    _form: '\Drupal\MY_MODULE\Form\MyCustomFormA'
    _title: 'My Custom Form A Form'
  requirements:
    _permission: 'access content'
MY_MODULE.my_custom_form_b:
  path: '/MY_MODULE/my_custom_form_b'
  defaults:
    _form: '\Drupal\MY_MODULE\Form\MyCustomFormB'
    _title: 'My Custom Form B Form'

  requirements:
    _permission: 'access content'
Jeirod avatar
cn flag
Thank you for your response, @Razeem. For the `MyCustomFormB.php`, should I also declare it on the routing.yml?
Razeem Ahmad avatar
ru flag
If you wan to display it in a specific page then yes
Jeirod avatar
cn flag
do you need `requirements` in each form. It gives me an 'HTTP ERROR 500'. I also tried to clear cache and it gives me the same error.
Razeem Ahmad avatar
ru flag
yes you may also add that
Jeirod avatar
cn flag
Thank you so much @Razeem
Razeem Ahmad avatar
ru flag
Welcome @JeirodAbogado.
Score:2
de flag

Yes, you can define as many forms in a module as you need.

If you want to include two forms into one page, you can do so by using the form builder in your controller to call both forms and build a render array.

Instead of using _form in your *.routing.yml file, you would instead use _controller, and define a controller callback rather than a form callback. Your controller would then be something like this:

class ExampleController extends Drupal\Core\Controller\ControllerBase {

  function examplePageCallback() {
    // Note - you should use dependency injection to inject this service, 
    // rather than this method.
    $form_builder = \Drupal::service('form_builder');

    $return [
      'form_a' => $form_builder->getForm('Drupal\[MODULE]\Form\FormA'),
      'form_b' => $form_builder->getForm('Drupal\[MODULE]\Form\FormB'),
    ];
  }
}
Jeirod avatar
cn flag
Thank you for your response, @Jaypan. I asked this question because I noticed that the FormOne can only be used and display. It seems that I can't use and display FormTwo. Should I declare each form in the routing.yml?
Jaypan avatar
de flag
If you want the form to be accessible at a route, yes, that is one way you can use the second form.
Jeirod avatar
cn flag
Is it possible to add both routes in the routing.yml? `mymodule.display_form:` ` path: '/mymodule'` ` defaults:` ` _form: '\Drupal\mymodule\Form\FormOne'` ` _form: '\Drupal\mymodule\Form\FormTwo'`
Kevin avatar
in flag
You need to build a controller that returns a render array containing both forms in that case.
Jaypan avatar
de flag
I've updated my answer.
Jeirod avatar
cn flag
@Jaypan, thanks for the update. I am planning to use each form in a separate page. Is it also possible?
Jaypan avatar
de flag
Yes, in that case you would use the method Razeem has posted as an answer. His answer is how to have separate pages for each form. My answer is how to have two forms on one page.
Jeirod avatar
cn flag
Thank you for the solution. @Jaypan
Jaypan avatar
de flag
You should mark his answer as correct, but give both of us an upvote for our answers. That's the custom here :) Welcome.
Jeirod avatar
cn flag
Sure @Jaypan, both answers are helpful. Thank you so much.
Jeirod avatar
cn flag
Thank you also @Kevin for the additional info
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.