Score:1

The form argument Drupal\terminal\Form\TerminalCliForm is not a valid form

cl flag

I've read through other questions regarding the same error message. They all seem to end up being due to typos or the file path. I don't believe that is the case here, but we'll see. I have a working instance of the same code aside from the names, which is all the more confusing.

I enable the module below. Select place its block. And then when I hit the page, receive:

InvalidArgumentException: The form argument Drupal\terminal\Form\TerminalCliForm is not a valid form. in Drupal\Core\Form\FormBuilder->getFormId() (line 197 of core/lib/Drupal/Core/Form/FormBuilder.php).

I've verified that getFormId receives an empty form_id. I've read that this error message indicates not a bad form in those specific cases but a form file that cannot be found. I'm not certain which is the case, here.

/modules/custom/terminal/src/Form/TerminalCliForm

namespace Drupal\terminal\Form;

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

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

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

  /**
   * {@inheritdoc}
  */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $form['#attached']['library'][] = 'terminal/terminal';
    $form['canvas'] = [
      '#type' => 'textarea',
    ];
    $form['commandline'] = [
      '#type' => 'textfield',
    ];
    return $form;
  }

  /**
   * {@inheritdoc}
  */
  public function validateForm(array &$form, FormStateInterface $form_state) {
    // Nothing.
  }

  /**
   * {@inheritdoc}
  */
  public function submitForm(array &$form, FormStateInterface $form_state) {
  }

}

/modules/custom/terminal/src/Plugin/Block/TerminalCliBlock - I've tried this with and without the use statement for the form class. In the other module that works correctly, it is present.

    namespace Drupal\terminal\Plugin\Block;

    use Drupal\Core\Block\BlockBase;
    use Drupal\terminal\Form\TerminalCliForm;

    /**
     * Provides a 'CliBlock' block.
     *
     * @Block(
     *   id = "terminal_cli_block",
     *   admin_label = @Translation("CLI block"),
     *   category = @Translation("Engine block")
     * )
    */

    class TerminalCliBlock extends BlockBase {

     /**
      * {@inheritdoc}
     */
     public function build() {

       $form = \Drupal::formBuilder()->getForm(TerminalCliForm::class);

       return $form;
     }
   }
vg flag
Have you copy pasted the filenames (and location) or typed it? (I would bet there is a typo in the folder or file names ;))
cl flag
web/modules/custom/terminal/src/Form/TerminalCliForm.php and web/modules/custom/terminal/src/Plugin/Block/TerminalCliBlock.php pasted from PhpStorm.
Jaypan avatar
de flag
This file: `/modules/custom/terminal/src/Form/TerminalCliForm` should have the file extension `.php` added to it.
cl flag
They both have a .php extension, as pasted above from PhpStorm
cn flag
Seems likely PHP can't find the class (`class_exists()` is failing in `FormBuilder::getFormId()`). If the file names are definitely correct try restarting the web server/PHP service - APC can sometimes get confused and need a reset
cl flag
That did it @Clive. Clearing cache, no, but stopping and restarting docksal, yes! Please post it as an answer.
MrUpsidown avatar
eg flag
Quite unbeliveable... but indeed, I had the same problem. Cleared the caches multiple times, checked the spelling and paths multiple times. Everything looked just right. I then stopped my Docker containers and restarted them and it worked right away :/ Very weird as I never had to do such a thing to fix a Drupal or PHP issue.
Web Assistant avatar
cn flag
Same thing just happened to me. Restarting Lando fixed the issue. Very weird.
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.