0

I'm trying to fix a form that was created using a twig template. The form was not working because there was no action, nor was the form created using FormStateInterface. It's just a block that is rendering a template.

In this module there is a controller and in this controller a method that handles the form submission using \Drupal::request()->request->all() which should be the same as using $_POST[''], correct?

Could I not just do something like this:

<form method="POST" action="/route/to/controller/method">

if I'm wanting to use a custom form template?

I would really just like to know if this is an acceptable practice in the Drupal universe.

4
  • 2
    There are so, so many reasons to use the API over manually creating HTML for forms - automatic CSRF protection, ability for other modules to intuitively alter what you've provided/changed, easy and pluggable validation/submission, a myriad of elements already available with sensible and useful properties, to name just a few. Yes, you can use plain HTML and plain PHP if you want to, it's your dime after all. But if you do, one would have to ask: why are you using a framework in the first place?
    – Clive
    Commented Jan 14, 2019 at 15:56
  • 2
    You should not be creating HTML forms ad-hoc in Twig templates or otherwise, it simply won't play well when using Drupal. Of course, it is possible, if you want to read PHP superglobals and all that, but you will spend 10x the time instead of just using the Form API and different services. If this were a Form API generated form, all you would need to do is add in the submit function a $form_state->setRedirect('route...');. But since its a vanilla HTML form, Drupal has no idea it even exists. Custom form templates are secondary.
    – Kevin
    Commented Jan 14, 2019 at 15:57
  • Thanks for the answers. So using FormStateInterface is the standard way to create all forms in Drupal. That is all that I was wanting to know.I would give some upvotes, but I don't have the rep. Commented Jan 14, 2019 at 18:34
  • You’d want to extend FormBase. Check the docs for examples on how to start.
    – Kevin
    Commented Jan 15, 2019 at 15:29

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.